Java Reference
In-Depth Information
NOTE
Server-side async response processing is only meant for a specific small subset of applications.
Asynchronous doesn't necessarily mean automatic scalability. For the typical web app, using server
asynchronous response processing will only complicate your code and make it harder to maintain. It
may even hurt performance.
AsyncResponse API
To use server-side async response processing, you interact with the
AsyncResponse
inter-
face:
package
package
javax
.
ws
.
rs
.
container
;
public
public interface
interface
AsyncResponse
AsyncResponse
{
boolean
boolean
resume
(
Object response
);
boolean
boolean
resume
(
Throwable response
);
...
}
You get access to an
AsyncResponse
instance by injecting it into a JAX-RS method using
the
@Suspended
annotation:
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
) {
}
}
Here we have our very familiar
OrderResource
. Order submission has been turned into an
asynchronous operation. When you inject an instance of
AsyncResponse
using the
@Suspen-
ded
annotation, the HTTP request becomes suspended from the current thread of execution.
In this particular example, the
OrderResource.submit()
method will never send back a re-
sponse to the client. The client will just time out with an error condition. Let's expand on this
example:
