users@jersey.java.net

[Jersey] Re: Observable resources

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 29 Aug 2014 13:35:37 +0200

Hi Andrea,

What you are trying to do is not going to be particularly straightforward, I'm afraid :)

You would need to:
1. add support for detecting that a method returning Observable needs to be handled as async case. This would need to be somehow propagated into ResourceMethod resource model class.
2. modify Jersey ResourceMethodInvoker.apply method to detect this special case and behave in the way similar to handling managed async use case (i.e. return null from the apply method and rely on some async completion; you would essentially need to make sure that the ResourceMethodInvoker.invoke() method functionality will all get invoked as part of the last Observable completion handler)
3. Implement your own ResourceMethodDispatcher.Provider or ResourceMethodInvocationHandlerProvider (I would need to think more to be sure which one would work best in this case) and register it in the system.

HTH,
Marek

On 26 Aug 2014, at 09:48, Andrea Ratto <andrearatto_liste_at_yahoo.it> wrote:

> 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
>
>
>
>