users@jersey.java.net

[Jersey] R: Re: Observable resources

From: Andrea Ratto <andrearatto_liste_at_yahoo.it>
Date: Tue, 26 Aug 2014 08:48:56 +0100

Hi Libor,

ManagedAsync is not the helpful here: the resource method call is very quick, since it just returns the Obsevable and its chain of subscribers.
Any long operation is already run in different threads-.

My problem is adding piece of code that has access to the return of the resource method, like a wrapper.

Something like this:

AsyncResponse ar = fromSomewhere.getAsyncResponse();
Observable<?> o = resourceMethod.invoke(); // or inflector.call() or what is it?
o.subscribe(s -> ar.resume(s)); // error handling omitted
 
A facility for doing this, I cannot find in the code base. Any more pointers? Thanks

--------------------------------------------
Lun 25/8/14, Libor Kramolis <libor.kramolis_at_oracle.com> ha scritto:

 Oggetto: [Jersey] Re: Observable resources
 A: andrearatto_at_yahoo.it
 Cc: users_at_jersey.java.net
 Data: Lunedì 25 agosto 2014, 21:29
 
 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.javaImpl
 start point:https://github.com/jersey/jersey/blob/54e27f31205de49004331db2d714c1be9a52bb76/core-server/src/main/java/org/glassfish/jersey/server/model/IntrospectionModeller.java#L288Example: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