Interface AsyncResponse


public interface AsyncResponse
An injectable JAX-RS asynchronous response that provides means for asynchronous server side response processing.

A new instance of AsyncResponse may be injected into a resource or sub-resource method parameter using the @Suspended annotation.

Each asynchronous response instance is bound to the running request and can be used to asynchronously provide the request processing result or otherwise manipulate the suspended client connection. The available operations include:
  • updating suspended state data (time-out value, response ...)
  • resuming the suspended request processing
  • canceling the suspended request processing

Following example demonstrates the use of the AsyncResponse for asynchronous HTTP request processing:

 @Path("/messages/next")
 public class MessagingResource {
     private static final BlockingQueue<AsyncResponse> suspended = new ArrayBlockingQueue<AsyncResponse>(5);

     @GET
     public void readMessage(@Suspended AsyncResponse ar) throws InterruptedException {
         suspended.put(ar);
     }

     @POST
     public String postMessage(final String message) throws InterruptedException {
         final AsyncResponse ar = suspended.take();
         ar.resume(message); // resumes the processing of one GET request
         return "Message sent";
     }
 }
 

If the asynchronous response was suspended with a positive timeout value, and has not been explicitly resumed before the timeout has expired, the processing will be resumed once the specified timeout threshold is reached, provided a positive timeout value was set on the response.

By default a timed-out asynchronous response is resumed with a WebApplicationException that has HTTP 503 (Service unavailable) error response status code set. This default behavior may be overridden by setting a custom time-out handler.

Since:
2.0