Topics

Returning CompletionStage from the resource method


Pavel Bucek
 

Dear experts,

as we already stated, we'd like to support returning CompletionStage from the resource mehod, corresponding spec issue is here:

https://github.com/jax-rs/api/issues/551

The only open issue is related to the thread on which the response will be processed (on which will be the "whenComplete" callback invoked").

Markus suggests that it could/should be done on servlet provided thread/executor, but there is no facility which would give as such thread. The case mentioned in the issue comment won't work, because "whenComplete" would be invoked on the thread which completed the CompletionStage (i.e.: there is no magic which would get back the thread, which was already handled over back to the servlet container). We could leave it like that (the application would need to have some thread which will complete the stage anyway) or we can say that container managed thread will be used, if available.

Please let us know if you have any suggestions or comments.

Thanks and regards,
Pavel


Sergey Beryozkin
 

Hi Pavel

I'd leave it up to the implementation to decide.

The other thing I was thinking about. On the client side we have RxInvoker which supports not only CompletableFuture. Should we have something like RxResponse such that not only CompletableFuture is supported ?

Thanks, Sergey




Pavel Bucek
 

Hi Sergey,

ad 1) the response processing will be done on the caller (completer) thread (so, completionStage#whenComplete(..), not #whenCompleteAsync(..)), simply because it works in a same way as when using AsyncResponse.

ad 2) good idea. I'm not sure whether we'll be able to do that now, but could you please file an issue? https://github.com/jax-rs/api/issues

Thanks,
Pavel


On 21/05/2017 13:47, Sergey Beryozkin wrote:

Hi Pavel

I'd leave it up to the implementation to decide.

The other thing I was thinking about. On the client side we have RxInvoker which supports not only CompletableFuture. Should we have something like RxResponse such that not only CompletableFuture is supported ?

Thanks, Sergey





 

Won't that work what I proposed in the issue tracker:

Giving it a second thought I wonder whether Servlet's AsyncContext wouldn't provide what I proposed originally: A handle to the "original" executor (not in the sense of Executor class, but in the sense of separation of concerns).

The servlet could invoke JAX-RS using request.startAsync().start(r), where r first invokes JAX-RS, gets the resulting CompletionStage, and adds to that whenComplete((o,e) ->

{...; asyncContext.complete()}

);

I assume that AsyncContext.complete() will in turn send the HttpServletResponse using the Servlet's own thread pool.