Top Description Fields Methods
javax.ws.rs.container

public Interface AsyncResponse

Imports
java.util.Collection, .Date, .Map, java.util.concurrent.TimeUnit

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:

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 javax.ws.rs.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.

Author
Marek Potociar
Since
2.0

Field Summary

Modifier and TypeField and Description
public static final long
NO_TIMEOUT

Constant specifying no suspend timeout value.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the request processing has been cancelled, returns false in case the request processing is not suspended and could not be cancelled and is not cancelled already.
cancel
()

Cancel the suspended request processing.

public boolean

Returns:

true if the request processing has been cancelled, returns false in case the request processing is not suspended and could not be cancelled and is not cancelled already.
cancel
(int
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.
retryAfter
)

Cancel the suspended request processing.

public boolean

Returns:

true if the request processing has been cancelled, returns false in case the request processing is not suspended and could not be cancelled and is not cancelled already.
cancel
(Date
a date that indicates how long the service is expected to be unavailable to the requesting client.
retryAfter
)

Cancel the suspended request processing.

public boolean

Returns:

true if this task was canceled before completion.
isCancelled
()

Check if the asynchronous response instance has been cancelled.

public boolean

Returns:

true if this execution context has finished processing.
isDone
()

Check if the processing of a request this asynchronous response instance belongs to has finished.

public boolean

Returns:

true if this asynchronous response is in a suspend state, false otherwise.
isSuspended
()

Check if the asynchronous response instance is in a suspended state.

public Collection<Class<?>>

Returns:

collection of registered callback interfaces. If the callback class does not implement any recognized callback interfaces, the returned collection will be empty.
register
(Class<?>
callback class.
callback
)

Register an asynchronous processing lifecycle callback class to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

public Map<Class<?>, Collection<Class<?>>>

Returns:

map of registered classes and the callback interfaces registered for each class. If a callback class does not implement any recognized callback interfaces, the associated collection of registered interfaces for the class will be empty.
register
(Class<?>
callback class.
callback
,
Class<?>...
additional callback classes.
callbacks
)

Register asynchronous processing lifecycle callback classes to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

public Collection<Class<?>>

Returns:

collection of registered callback interfaces. If the callback class does not implement any recognized callback interfaces, the returned collection will be empty.
register
(Object
callback instance implementing one or more of the recognized callback interfaces.
callback
)

Register an asynchronous processing lifecycle callback instance to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

public Map<Class<?>, Collection<Class<?>>>

Returns:

map of registered classes and the callback interfaces registered for each class. If a callback class does not implement any recognized callback interfaces, the associated collection of registered interfaces for the class will be empty.
register
(Object
callback instance.
callback
,
Object...
additional callback instances.
callbacks
)

Register an asynchronous processing lifecycle callback instances to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

public boolean

Returns:

true if the request processing has been resumed, returns false in case the request processing is not suspended and could not be resumed.
resume
(Object
data to be sent back in response to the suspended request.
response
)

Resume the suspended request processing using the provided response data.

public boolean

Returns:

true if the response has been resumed, returns false in case the response is not suspended and could not be resumed.
resume
(Throwable
an exception to be raised in response to the suspended request.
response
)

Resume the suspended request processing using the provided throwable.

public boolean

Returns:

true if the suspend time out has been set, returns false in case the request processing is not in the suspended state.
setTimeout
(long
suspend timeout value in the give time unit. Value lower or equal to 0 causes the context to suspend indefinitely.
time
,
TimeUnit
suspend timeout value time unit.
unit
)

Set/update the suspend timeout.

public void
setTimeoutHandler(TimeoutHandler
response time-out handler.
handler
)

Set/replace a time-out handler for the suspended asynchronous response.

Field Detail

NO_TIMEOUTback to summary
public static final long NO_TIMEOUT

Constant specifying no suspend timeout value.

Method Detail

cancelback to summary
public boolean 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 cancelled or resumed has no effect and the method call is ignored while returning true, in case the request has been cancelled previously. Otherwise, in case the request has been resumed regularly (using a resume(...) method) or resumed due to a time-out, method returns false.

Returns:boolean

true if the request processing has been cancelled, returns false in case the request processing is not suspended and could not be cancelled and is not cancelled already.

See Also
cancel(int), cancel(java.util.Date)
cancelback to summary
public boolean 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 cancelled or resumed has no effect and the method call is ignored while returning true, in case the request has been cancelled previously. Otherwise, in case the request has been resumed regularly (using a resume(...) method) or resumed due to a time-out, method returns false.

Parameters
retryAfter:int

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.

Returns:boolean

true if the request processing has been cancelled, returns false in case the request processing is not suspended and could not be cancelled and is not cancelled already.

See Also
cancel, cancel(java.util.Date)
cancelback to summary
public boolean 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 cancelled or resumed has no effect and the method call is ignored while returning true, in case the request has been cancelled previously. Otherwise, in case the request has been resumed regularly (using a resume(...) method) or resumed due to a time-out, method returns false.

Parameters
retryAfter:Date

a date that indicates how long the service is expected to be unavailable to the requesting client.

Returns:boolean

true if the request processing has been cancelled, returns false in case the request processing is not suspended and could not be cancelled and is not cancelled already.

See Also
cancel, cancel(int)
isCancelledback to summary
public boolean isCancelled()

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

Returns:boolean

true if this task was canceled before completion.

See Also
isSuspended(), isDone()
isDoneback to summary
public 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:boolean

true if this execution context has finished processing.

See Also
isSuspended(), isCancelled()
isSuspendedback to summary
public 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:boolean

true if this asynchronous response is in a suspend state, false otherwise.

See Also
isCancelled(), isDone()
registerback to summary
public Collection<Class<?>> register(Class<?> callback)

Register an asynchronous processing lifecycle callback class to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

Parameters
callback:Class<?>

callback class.

Returns:Collection<Class<?>>

collection of registered callback interfaces. If the callback class does not implement any recognized callback interfaces, the returned collection will be empty.

Exceptions
NullPointerException:
in case the callback class is null.
registerback to summary
public Map<Class<?>, Collection<Class<?>>> 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.

Parameters
callback:Class<?>

callback class.

callbacks:Class<?>[]

additional callback classes.

Returns:Map<Class<?>, Collection<Class<?>>>

map of registered classes and the callback interfaces registered for each class. If a callback class does not implement any recognized callback interfaces, the associated collection of registered interfaces for the class will be empty.

Exceptions
NullPointerException:
in case any of the callback classes is null.
registerback to summary
public Collection<Class<?>> register(Object callback)

Register an asynchronous processing lifecycle callback instance to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.

Parameters
callback:Object

callback instance implementing one or more of the recognized callback interfaces.

Returns:Collection<Class<?>>

collection of registered callback interfaces. If the callback class does not implement any recognized callback interfaces, the returned collection will be empty.

Exceptions
NullPointerException:
in case the callback instance is null.
registerback to summary
public Map<Class<?>, Collection<Class<?>>> 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.

Parameters
callback:Object

callback instance.

callbacks:Object[]

additional callback instances.

Returns:Map<Class<?>, Collection<Class<?>>>

map of registered classes and the callback interfaces registered for each class. If a callback class does not implement any recognized callback interfaces, the associated collection of registered interfaces for the class will be empty.

Exceptions
NullPointerException:
in case any of the callback instances is null.
resumeback to summary
public boolean resume(Object response)

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 asynchronous response must be still in a suspended state for this method to succeed.

By executing this method, the request is guaranteed to complete either successfully or with an error. The data processing by the JAX-RS runtime follows the same path as it would for the response data returned synchronously by a JAX-RS resource, except that unmapped exceptions are not re-thrown by JAX-RS runtime to be handled by a hosting I/O container. Instead, any unmapped exceptions are propagated to the hosting I/O container via a container-specific callback mechanism. Depending on the container implementation, propagated unmapped exceptions typically result in an error status being sent to the client and/or the connection being closed.

Parameters
response:Object

data to be sent back in response to the suspended request.

Returns:boolean

true if the request processing has been resumed, returns false in case the request processing is not suspended and could not be resumed.

See Also
resume(Throwable)
resumeback to summary
public boolean resume(Throwable response)

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.

By executing this method, the request is guaranteed to complete either successfully or with an error. The throwable processing by the JAX-RS runtime follows the same path as it would for the response data returned synchronously by a JAX-RS resource, except that unmapped exceptions are not re-thrown by JAX-RS runtime to be handled by a hosting I/O container. Instead, any unmapped exceptions are propagated to the hosting I/O container via a container-specific callback mechanism. Depending on the container implementation, propagated unmapped exceptions typically result in an error status being sent to the client and/or the connection being closed.

Parameters
response:Throwable

an exception to be raised in response to the suspended request.

Returns:boolean

true if the response has been resumed, returns false in case the response is not suspended and could not be resumed.

See Also
resume(Object)
setTimeoutback to summary
public boolean setTimeout(long time, TimeUnit unit)

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

suspend timeout value in the give time unit. Value lower or equal to 0 causes the context to suspend indefinitely.

unit:TimeUnit

suspend timeout value time unit.

Returns:boolean

true if the suspend time out has been set, returns false in case the request processing is not in the suspended state.

setTimeoutHandlerback to summary
public 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

  • resuming the suspended response
  • cancelling the suspended response
  • extending the suspend period by setting a new suspend time-out

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

Parameters
handler:TimeoutHandler

response time-out handler.