Java Reference
In-Depth Information
@GET
public void longRunningOp(@Suspended final AsyncResponse ar) {
mes.submit(
new Runnable() {
public void run() {
// Long running operation
ar.resume("Return asynchronously!");
}
});
}
}
An EJB resource can also be utilized in an asynchronous manner, although coded a bit differently. A web resource
method within an EJB can denoted with the @Asynchronous annotation to signify that the method contents are to
be processed asynchronously. The method should accept the same @Suspended AsyncResponse argument, and that
argument's resume method should be invoked at the end of the long-running process. There is no need to create a
Runnable or work with an Executor in an EJB web resource because the @Asynchronous annotation takes care of that
detail. The EJB container automatically allocates the resources necessary to handle the asynchronous operation. The
following example demonstrates the coding of an asynchronous method within an EJB:
...
@GET @Asynchronous
public void longRunningOp(@Suspended AsyncResponse ar){
// Long running operation
ar.resume("Return asynchronously");
}
Note that there is no special treatment that needs to be done, with regard to threading or utilization of Runnable .
Everything just works because the EJB container takes care of the concurrency.
Resource Context
The JAX-RS API provides contexts that can be used to obtain metadata information about the application deployment
context and/or the context of individual requests. To obtain metadata regarding context, the required metadata can
be injected into a web resource by specifying the @Context annotation along with the context type you want to inject.
For instance, if there is a requirement to obtain metadata regarding an application's configuration, an instance of the
application-supplied Application subclass can be injected into a web resource class field or method parameter by
specifying the @Context annotation.
@Context Application app;
Other contexts are made available via the @Context annotation as well. Table 8-4 describes the different
context types.
 
Search WWH ::




Custom Search