Sun GlassFish Enterprise Server |
Servlet
The servlet is responsible for asynchronous request processing.
Deployment Descriptor
- In the
init
method, start a thread to read message from themessageQueue
and then process the message for eachAsyncContext
in the queue as follows:
PrintWriter acWriter = ac.getResponse().getWriter();
acWriter.println(cMessage);
acWriter.flush();
The preceding code writes a message to each asynchronous request corresponding to
AsyncContext
.
- In
doGet
, set the timeout to ten minutes, start asynchronous, and addAsyncContext
to a queue as follows:
final AsyncContext ac = req.startAsync();
ac.setTimeout(10 * 60 * 1000);
queue.add(ac);
Registers
AsyncListener
with theAsyncContext
. In this case, only clean up theAsyncContext
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
doPost
, put a message to the messageQueue. This message is picked up in the thread that starts in theinit
method.
sun-web.xml
) for this
example.
Perform the following steps to build, deploy, and run the application:
app_dir
is the sample application base
directory: samples_install_dir/javaee6/web/servlet/async-request-war
Change directory to app_dir.
all
target.
app_dir> ant
all
You can replace the ant all
command with the
following set of
commands:
app_dir> ant
default
compiles and packages the application
app_dir> ant
deploy
deploys it to application server
System Message:
userA has joined.
Similarly, login as "userB" in Browser B.
userA:
Hello
Try the same step in Browser B also.
undeploy
to undeploy the application.
app_dir> ant
undeploy
clean
to remove the temporary directories
like build and dist.
app_dir> ant
clean
Perform the following steps to build, deploy, and run the application using NetBeans IDE:
samples_install_dir/javaee6/web/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 © 2009 Sun Microsystems, Inc. All rights reserved.