javax.ws.rs.container
Interface AsyncResponse

All Known Subinterfaces:
ResteasyAsynchronousResponse
All Known Implementing Classes:
AbstractAsynchronousResponse, SynchronousExecutionContext.SynchronousAsynchronousResponse

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 @Suspend 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:

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);
         return 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
Author:
Marek Potociar

Field Summary
static long NO_TIMEOUT
          Constant specifying no suspend timeout value.
 
Method Summary
 void cancel()
          Cancel the suspended request processing.
 void cancel(Date retryAfter)
          Cancel the suspended request processing.
 void cancel(int retryAfter)
          Cancel the suspended request processing.
 boolean isCancelled()
          Check if the asynchronous response instance has been cancelled.
 boolean isDone()
          Check if the processing of a request this asynchronous response instance belongs to has finished.
 boolean isSuspended()
          Check if the asynchronous response instance is in a suspended state.
 boolean register(Class<?> callback)
          Register an asynchronous processing lifecycle callback class to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.
 boolean[] register(Class<?> callback, Class<?>... callbacks)
          Register asynchronous processing lifecycle callback classes to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.
 boolean register(Object callback)
          Register an asynchronous processing lifecycle callback instance to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.
 boolean[] register(Object callback, Object... callbacks)
          Register an asynchronous processing lifecycle callback instances to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.
 void resume(Object response)
          Resume the suspended request processing using the provided response data.
 void resume(Throwable response)
          Resume the suspended request processing using the provided throwable.
 void setTimeout(long time, TimeUnit unit)
          Set/update the suspend timeout.
 void setTimeoutHandler(TimeoutHandler handler)
          Set/replace a time-out handler for the suspended asynchronous response.
 

Field Detail

NO_TIMEOUT

static final long NO_TIMEOUT
Constant specifying no suspend timeout value.

See Also:
Constant Field Values
Method Detail

resume

void resume(Object response)
            throws IllegalStateException
Resume the suspended request processing using the provided response data.

The provided response data can be of any Java type that can be returned from a JAX-RS resource method. The processing of the data by JAX-RS framework follows the same path as it would for the response data returned synchronously by a JAX-RS resource method. The asynchronous response must be still in a suspended state for this method to succeed.

Parameters:
response - data to be sent back in response to the suspended request.
Throws:
IllegalStateException - in case the response is not suspended.
See Also:
resume(Throwable)

resume

void resume(Throwable response)
            throws IllegalStateException
Resume the suspended request processing using the provided throwable.

For the provided throwable same rules apply as for an exception thrown by a JAX-RS resource method. The processing of the throwable by JAX-RS framework follows the same path as it would for any exception thrown by a JAX-RS resource method. The asynchronous response must be still in a suspended state for this method to succeed.

Parameters:
response - an exception to be raised in response to the suspended request.
Throws:
IllegalStateException - in case the response is not suspended.
See Also:
resume(Object)

cancel

void cancel()
Cancel the suspended request processing.

When a request processing is cancelled using this method, the JAX-RS implementation MUST indicate to the client that the request processing has been cancelled by sending back a HTTP 503 (Service unavailable) error response.

Invoking a cancel(...) method multiple times to cancel request processing has the same effect as canceling the request processing only once. Invoking a cancel(...) method on an asynchronous response instance that has already been resumed has no effect and the method call is ignored. Once the request is canceled, any attempts to suspend or resume the asynchronous response will result in an IllegalStateException being thrown.

See Also:
cancel(int), cancel(java.util.Date)

cancel

void cancel(int retryAfter)
Cancel the suspended request processing.

When a request processing is cancelled using this method, the JAX-RS implementation MUST indicate to the client that the request processing has been cancelled by sending back a HTTP 503 (Service unavailable) error response with a Retry-After header set to the value provided by the method parameter.

Invoking a cancel(...) method multiple times to cancel request processing has the same effect as canceling the request processing only once. Invoking a cancel(...) method on an asynchronous response instance that has already been resumed has no effect and the method call is ignored. Once the request is canceled, any attempts to suspend or resume the asynchronous response will result in an IllegalStateException being thrown.

Parameters:
retryAfter - a decimal integer number of seconds after the response is sent to the client that indicates how long the service is expected to be unavailable to the requesting client.
See Also:
cancel(), cancel(java.util.Date)

cancel

void cancel(Date retryAfter)
Cancel the suspended request processing.

When a request processing is cancelled using this method, the JAX-RS implementation MUST indicate to the client that the request processing has been cancelled by sending back a HTTP 503 (Service unavailable) error response with a Retry-After header set to the value provided by the method parameter.

Invoking a cancel(...) method multiple times to cancel request processing has the same effect as canceling the request processing only once. Invoking a cancel(...) method on an asynchronous response instance that has already been resumed has no effect and the method call is ignored. Once the request is canceled, any attempts to suspend or resume the asynchronous response will result in an IllegalStateException being thrown.

Parameters:
retryAfter - a date that indicates how long the service is expected to be unavailable to the requesting client.
See Also:
cancel(), cancel(int)

isSuspended

boolean isSuspended()
Check if the asynchronous response instance is in a suspended state. Method returns true if this asynchronous response is still suspended and has not finished processing yet (either by resuming or canceling the response).

Returns:
true if this asynchronous response is in a suspend state, false otherwise.
See Also:
isCancelled(), isDone()

isCancelled

boolean isCancelled()
Check if the asynchronous response instance has been cancelled. Method returns true if this asynchronous response has been canceled before completion.

Returns:
true if this task was canceled before completion.
See Also:
isSuspended(), isDone()

isDone

boolean isDone()
Check if the processing of a request this asynchronous response instance belongs to has finished. Method returns true if the processing of a request this asynchronous response is bound to is finished.

The request processing may be finished due to a normal termination, a suspend timeout, or cancellation -- in all of these cases, this method will return true.

Returns:
true if this execution context has finished processing.
See Also:
isSuspended(), isCancelled()

setTimeout

void setTimeout(long time,
                TimeUnit unit)
                throws IllegalStateException
Set/update the suspend timeout.

The new suspend timeout values override any timeout value previously specified. The asynchronous response must be still in a suspended state for this method to succeed.

Parameters:
time - suspend timeout value in the give time unit. Value lower or equal to 0 causes the context to suspend indefinitely.
unit - suspend timeout value time unit.
Throws:
IllegalStateException - in case the response is not suspended.

setTimeoutHandler

void setTimeoutHandler(TimeoutHandler handler)
Set/replace a time-out handler for the suspended asynchronous response.

The time-out handler will be invoked when the suspend period of this asynchronous response times out. The job of the time-out handler is to resolve the time-out situation by either

Note that in case the response is suspended indefinitely, the time-out handler may never be invoked.

Parameters:
handler - response time-out handler.

register

boolean register(Class<?> callback)
                 throws NullPointerException
Register an asynchronous processing lifecycle callback class to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

Parameters:
callback - callback class.
Returns:
true if the callback class was recognized and registered, false otherwise.
Throws:
NullPointerException - in case the callback class is null.
See Also:
ResumeCallback

register

boolean[] register(Class<?> callback,
                   Class<?>... callbacks)
                   throws NullPointerException
Register asynchronous processing lifecycle callback classes to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

Parameters:
callback - callback class.
callbacks - additional callback classes.
Returns:
a boolean array of the size equal to the number of registered callback classes. Each value in the array indicate whether the particular callback class was registered successfully or not.
Throws:
NullPointerException - in case any of the callback classes is null.
See Also:
ResumeCallback

register

boolean register(Object callback)
                 throws NullPointerException
Register an asynchronous processing lifecycle callback instance to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

Parameters:
callback - callback instance implementing one or more of the recognized callback interfaces.
Returns:
true if the callback class was recognized and registered, false otherwise.
Throws:
NullPointerException - in case the callback instance is null.
See Also:
ResumeCallback

register

boolean[] register(Object callback,
                   Object... callbacks)
                   throws NullPointerException
Register an asynchronous processing lifecycle callback instances to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

Parameters:
callback - callback instance.
callbacks - additional callback instances.
Returns:
a boolean array of the size equal to the number of registered callbacks. Each value in the array indicate whether the particular callback was registered successfully or not.
Throws:
NullPointerException - in case any of the callback instances is null.
See Also:
ResumeCallback


Copyright © 2013. All Rights Reserved.