Java Reference
In-Depth Information
public List<Student> getListOfAllStudentsAs() {
try{
Thread.sleep(20*1000);//20 seconds
}catch(Exception ex){}
TypedQuery<Student> query =
em.createQuery("SELECT s FROM Student s",
Student.class);
return query.getResultList();
}
Similarly, the JAX-RS servers are able to run processes asynchronously. The meth-
od that contains the instructions to perform the task asynchronously must inject an
object of type
AsyncResponse
as a method parameter with the
@Suspended
an-
notation. However, you should know that the asynchronous mode of the server dif-
fers from the asynchronous mode of the client; the former consists of suspending
the client connection from which the request was send during the processing of the
request before resuming it later through the
resume()
method of the object
Asyn-
cResponse
. The method itself will not run asynchronously. To make it asynchron-
ous, you must either delegate the process to a thread (that is what we did in the
getListOfAllStudentsAs2
method of the following example), or decorate it with
the
@Asynchronous
annotation. The following code demonstrates how to perform
asynchronous processing at the server side.
The following code is an example of the execution of a process asynchronously in a
JAX-RS server:
@Path("students")
@Stateless
@Produces({MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML})
public class StudentInformation {
@PersistenceContext(unitName =
"integrationPU")
private EntityManager em;
@Resource(lookup ="java:comp/