Java Reference
In-Depth Information
Internally, a
Response
object is built with a 503 status code. For
cancel()
methods that ac-
cept input, the parameter is used to initialize a
Retry-After
HTTP response header.
Status Methods
There's a few status methods on
AsyncResponse
that specify the state of the response:
public
public interface
interface
AsyncResponse
AsyncResponse
{
boolean
boolean
isSuspended
();
boolean
boolean
isCancelled
();
boolean
boolean
isDone
();
...
}
The
AsyncResponse.isCancelled()
method can be called to see if a
AsyncResponse
has
been cancelled.
isSuspended()
specifies whether or not the response can have
resume()
or
cancel()
invoked. The
isDone()
method tells you if the response is finished.
Timeouts
If an
AsyncResponse
is not resumed or cancelled, it will eventually time out. The default
timeout is container-specific. A timeout results in a 503, “Service Unavailable,” response
code sent back to the client. You can explicitly set the timeout by invoking the
setTimeout()
method:
response
.
setTimeout
(
5
,
TimeUnit
.
SECONDS
);
You can also register a callback that is triggered when a timeout occurs by implementing the
TimeoutHandler
interface. For example:
response
.
setTimeoutHandler
(
new
new
TimeoutHandler
{
void
void
handleTimeout
(
AsyncResponse response
) {
response
.
resume
(
Response
.
serverError
().
build
());
}
}
);
Here, instead of sending the default 503 response code to the client on a timeout, the ex-
ample registers a
TimeoutHandler
that sends a 500 response code instead.