2

I am having some questions about consuming a Spring WebFlux application from a Play Framework application, over HTTP, could you please provide some help?

Micro service A is a reactive Spring WebFlux, written in Java 8, SpringBoot 2.1.4, it is exposing this API:

@Autowired private ReactiveCustomerRepository customerRepository;

@GetMapping("/customers")
public Flux<Customer> getAllCustomers() {
    Flux<Customer> c = customerRepository.findAll().delayElements(Duration.ofMillis(5000));
    return c;
}

I would like to consume this, in a reactive way from a Play Framework micro service B, over HTTP.

Could you please provide some advices or a small snippet on how to achieve this please?

Thank you for your help.

PatPanda
  • 3,644
  • 9
  • 58
  • 154

1 Answers1

2

You can try to use different content-type that supports streaming - application/stream+json. Spring WebFlux will serialize individual Flux elements and send them one by one over the wire. Check out the following SO thread about it: Spring WebFlux Flux behavior with non streaming application/json

On the play-ws side you should be able to receive this data as Source[T].

Ilya Zinkovich
  • 4,082
  • 4
  • 25
  • 43
  • I have found interesting resource about it here [Serving large datasets with Spring WebFlux](https://kamilszymanski.github.io/serving-large-datasets-with-spring-webflux/). However the difference is in serving in chunks vs serving all at once, while the pitfalls of HTTP prevails in comparison to HTTP/2 or GRPC. Seems to be good fit for WebFlux though as it provides data in asynchronous manner allowing easy streaming chunk by chunk. If the Play WS would process the output in stream fashion this will be certainly optimisation, – Xarvalus May 13 '19 at 19:04
  • There is no doubt that HTTP/2, gRPC or WebSockets are practically a better fit for reactive applications. However, even with these transports one should always validate if they're a good fit for a particular use case. – Ilya Zinkovich May 14 '19 at 09:40
  • Hello Ilya, this is quite interesting. Could you please suggest an example with Source[T] consuming the Spring Flux? – PatPanda May 14 '19 at 11:36
  • @PatPatPat It should be no different that consuming other stream in play-ws. Check out the following doc: https://www.playframework.com/documentation/2.6.x/ScalaWS#Processing-large-responses – Ilya Zinkovich May 14 '19 at 12:27
  • Hello Ilya, and how about Play 2.4? With Promise it is becoming a bit more difficult. – PatPanda May 14 '19 at 13:58
  • There is a similar API in 2.4 https://www.playframework.com/documentation/2.4.x/ScalaWS#Processing-large-responses – Ilya Zinkovich May 14 '19 at 14:52