| Java EE 7 SDK |
This is a simple servlet application using the Asynchronous Processing API.
Servlet
The servlet processes requests asynchronously.
In the init method, the servlet starts a thread that reads
messages from the messageQueue and then processes the
message for each AsyncContext in the queue as follows:
PrintWriter acWriter = ac.getResponse().getWriter(); acWriter.println(cMessage); acWriter.flush();
The preceding code writes a message for each asynchronous request
by getting a writer from its AsyncContext.
In the doGet method, the servlet sets the timeout to ten minutes,
starts the asynchronous processing, and adds the AsyncContext to a queue as follows:
final AsyncContext ac = req.startAsync(); ac.setTimeout(10 * 60 * 1000); queue.add(ac);
The servlet registers an AsyncListener for each AsyncContext.
In this case, the listener methods remove the AsyncContext from the queue.
ac.addListener(new AsyncListener() {
public void onComplete(AsyncEvent event) throws IOException {
queue.remove(ac);
}
public void onTimeout(AsyncEvent event) throws IOException {
queue.remove(ac);
}
public void onError(AsyncEvent event) throws IOException {
queue.remove(ac);
}
public void onStartAsync(AsyncEvent event) throws IOException {
}
});
In the doPost method, the servlet adds a message to the
message queue. The message is picked up in the thread started in
the init method.
Deployment Descriptor
By using annotations, the deployment descriptor is no longer required
GlassFish Server Specific Deployment Configuration
There is no need to define any Sun GlassFish Enterprise Server specific
deployment descrpitor (sun-web.xml) for this
example.
This sample application demonstrates the following key features:
Perform the following steps to build, deploy, and run the application:
app_dir is the sample application base
directory: samples_install_dir/servlet/async-request-war.Change directory to app_dir.
mvn target:Use the command below to run this sample which is using Cargo framework:
app_dir>
mvn clean verify cargo:run
You can point Cargo to an already installed and running Glassfish server:
app_dir> mvn clean verify cargo:run -Dglassfish.home=$<glassfish_dir> (e.g. ../glassfish4)
You can also build, deploy the sample application without Cargo:
app_dir> mvn install
app_dir> asadmin deploy ./target/<app_name>.war
http://<javaee.server.name>:<javaee.server.port>/async-request-waruserA.
Click the Login button. The following message is displayed in Browser A
and Browser B:
System Message:
userA has joined.
Similarly, log in as userB in Browser B.
Post Message button. The following
message appears in both Browser A and Browser
B:
userA:
Hello
Try the same step in Browser B.
app_dir>
asadmin undeploy <app_name>
clean to remove the temporary directories
like /target.
app_dir> mvn
clean
Perform the following steps to build, deploy, and run the application using NetBeans IDE:
samples_install_dir/servlet/ directory, select async-request-war, and click Open Project.async-request-war and select Run to build, deploy, and run the project.If you have problems when running the application, refer the troubleshooting document.
Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.