[jax-rs-spec users] Re: Non blocking IO: Publishers and Subscribers

From: <>
Date: Fri, 24 Mar 2017 10:48:25 +0000 (UTC)

There are two issues with the code below:

1. In a reactive flow, the user code never triggers subscription.
   That’s a responsibility of the resource management component
   (or simply the caller of the ex(Flow.Source) method).
2. A Publisher does not start doing anything before subscription
   and it does not start emitting before the subscriber requests
   data (Subscription.request(n)).
   Background: The Publisher may buffer incoming data before data
   is actually requested but that's an impl detail of a Publisher.
A fixed version would look like:
(I used Flow.Publisher to stick with Java 9's names)

@POST @Path("/ex2")
public Flow.Publisher<AnotherPOJO> ex(Flow.Publisher<POJO> entity) {

     Flow.Publisher<AnotherPOJO> result = …; // something like
                  // result = Flux.fromPublisher(entity).map(it -> new
                  // result = Flowable.fromPublisher(entity).map(it ->
new AnotherEntity(it));

     return result;

From a framework API perspective, you work only with Publisher's.
Everything else (Processor) is up to what users do in their code
or what the implementation does.