users@jersey.java.net

[Jersey] Re: Scaling with AsyncResponse

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Fri, 12 Jul 2013 18:17:59 +0200

Hi Robert,

On Jul 12, 2013, at 5:44 PM, Robert DiFalco <robert.difalco_at_gmail.com> wrote:

> I have a couple of questions. With Jersey 1.x I am using a pretty typical pattern for scaling. Basically, for any request that can take longer than ~1 second I return a polling URL. The client then calls this URL to get the request status. If I am still processing the operation (but the request is still valid) I return a 200. If the operation is done I return a 303 with a new URL to the ready resource. In most cases I put the resource in REDIS and the URL contains the key to the JSON in REDIS. This is obviously important because I need the REST server to be stateless so it can scale out. For example, the polling request could come once on one server and again on another server. The SEE OTHER could end up directing the client to another server than it was issued from. Make sense?

Right, this makes sense.

>
> So my question is this. Does Jersey 2.0 give me anything to make this pattern easier to implement? Additionally, what is AsyncResponse used for and does it help me at all here?
>

AsyncResponse (see detailed JavaDoc at [1]) should help you avoid the polling part. I.e. instead of referring the client
to the other polling resource, you just suspend the request processing and return 303 once you got the REDIS URI.

> These stateless Async calls can be tedious to implement since I have to distribute the status (processing, gone, done, etc) as well as the resource so it is available to each dyno that runs my REST service.
>
> Is it true that AsyncResponse keeps the client connection for the complete async operation? If so then I'm having a hard time understanding when I should use it over the above pattern.

The client connection will be kept, what you save here is the request processing thread cycles.
What happens is once you suspend the actual request, you unblock the processing thread,
so that it could be utilised to process other requests.

HTH,

~Jakub

>
> Thanks!

[1]https://jersey.java.net/apidocs/latest/jersey/javax/ws/rs/container/AsyncResponse.html