users@jersey.java.net

[Jersey] Re: Observable resources

From: Libor Kramolis <libor.kramolis_at_oracle.com>
Date: Mon, 25 Aug 2014 21:29:35 +0200

Hi Andrea.

That’s great you are trying that. Reative is future. ;-)

Quick answer. Try to look at Jersey Managed Async support. It is Jersey specific more straightforward JAX-RS Async support.

Jersey API:
https://github.com/jersey/jersey/blob/54e27f31205de49004331db2d714c1be9a52bb76/core-server/src/main/java/org/glassfish/jersey/server/ManagedAsync.java
Impl start point:
https://github.com/jersey/jersey/blob/54e27f31205de49004331db2d714c1be9a52bb76/core-server/src/main/java/org/glassfish/jersey/server/model/IntrospectionModeller.java#L288
Example:
https://github.com/jersey/jersey/blob/master/examples/server-async-managed/src/main/java/org/glassfish/jersey/examples/server/async/managed/ChatResource.java

And let us know how successful you are. I guess this is good area for contribution at least to incubator.

Best regards,
-lk


On 25 Aug 2014, at 14:46, Andrea Ratto <andrearatto_liste_at_yahoo.it> wrote:

> Hello list!,
>
> I am trying to use RxJava Observables to handle various asynchronous
> operations
> in Jersey 2 app.
>
> Using async support from Jersey requires most resources to subscribe and
> complete the AsyncResponse when an observable does:
>
> @GET
> public void asyncGet(@Suspended final AsyncResponse asyncResponse) {
> myService.getSomethingAsObservable()
> .map(s -> s.doSomethingMore()
> .subscribe(s -> asyncResponse.resume(s)); // simplified
> }
>
> To do it elegantly, without boilerplate code, would require some magic
> annotation or something else to factor out this behaviour and basically
> just
> return an Observable in HTTP methods:
>
> @GET
> @SuspendedOnObservable
> public Observable<Object> asyncGet() {
> return myService.getSomethingAsObservable()
> .map(s -> s.doSomethingMore();
> }
>
> Doesn't it look great? Doing it requires solving two problems:
>
> 1. ensuring that the resource is handled asynchronously by Jersey.
> 2. running some code after the method to get the AsyncResponse and
> subscribe it
> the to the returned Observable.
>
> Number 1 I think I can solve inspecting the model.
>
> For the second problem, I am in need of some pointers.
> Thank you.
>
> Andrea
>
>