jersey ws 2.0 @suspended AsyncResponse, what does it do?

The @suspend annotation makes the caller actually wait until your done work. Lets say you have a lot of work to do on another thread. when you use jersey @suspend the caller just sits there and waits (so on a web browser they just see a spinner) until your AsyncResponse object returns data to it.

Imagine you had a really long operation you had to do and you want to do it on another thread (or multiple threads). Now we can have the user wait until we are done. Don't forget in jersey you'll need to add the " true" right in the jersey servlet definition in web.xml to get it to work.


@Suspended have more definite if you used it, else it not makes any difference of using it.let's talk about benefits of its.

  • @Suspended will pause/Suspend the current thread until it gets response,by default #NO_TIMEOUT no suspend timeout set. so it doesn't mean your request response thread get free and available for other.
  • Now Assume you want your service to be a response with some specific time, but the method you are calling from resource not guarantee the response time, then how will you manage your service response time.At that time you can set suspend timeout for your service using @Suspended , and even provide a fall back response when time get exceed.

Below is some sample of code for setting suspend/pause timeout

public void longRunningOperation(@Suspended AsyncResponse ar) {
 *      ar.setTimeoutHandler(customHandler);
 *      ar.setTimeout(10, TimeUnit.SECONDS);
 *      final String result = executeLongRunningOperation();
 *      ar.resume(result);
 *    }

for more details refer this