Java Reference
In-Depth Information
final
final
@Suspended
AsyncResponse response
) {
new
new
Thread
() {
public
public
void
void
run
() {
OrderConfirmation confirmation
=
orderProcessor
.
process
(
order
);
Response response
=
Response
.
ok
(
confirmation
,
MediaType
.
APPLICATION_XML_TYPE
)
.
build
();
response
.
resume
(
response
);
}
}.
start
();
}
}
In this example, we've manually created a
Response
. We set the entity to the
OrderCon-
firmation
and the content type to XML.
Exception Handling
When you invoke
AsyncResponse.resume(Object)
, the response filter and interceptor
tion is thrown by any one of these components, then the exception is handled in the same
way as its synchronous counterpart with one caveat. Unhandled exceptions are not propag-
ated, but instead the server will return a 500, “Internal Server Error,” back to the client.
Finally, the previous example is pretty simple, but what if it were possible for
orderPro-
cessor.process()
to throw an exception? We can handle this exception by using the
Asyn-
cResponse.resume(Throwable)
method:
import
import
javax.ws.rs.container.AsyncResponse
javax.ws.rs.container.AsyncResponse
;
import
import
javax.ws.rs.container.Suspended
javax.ws.rs.container.Suspended
;
@Path
(
"/orders"
)
public
public class
class
OrderResource
OrderResource
{
@POST
@Consumes
(
"application/json"
)
public
public
void
void
submit
(
final
final
Order order
,
final
final
@Suspended
AsyncResponse response
) {
new
new
Thread
() {
public
public
void
void
run
() {
OrderConfirmation confirmation
=
null
null
;
try
try
{
confirmation
=
orderProcessor
.
process
(
order
);