Java EE 7 SDK 

Samples Main Page

The Asynchronous Processing Servlet Sample Application

This is a simple servlet application using the Asynchronous Processing API.

Description

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.

Key Features

This sample application demonstrates the following key features:

Building, Deploying, and Running the Application

Perform the following steps to build, deploy, and run the application:

  1. Set up your build environment and configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/servlet/async-request-war.
  3. Change directory to app_dir.
  4. Build and deploy the sample application using the mvn target:
  5. 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

  6. Open two different browser windows to run this application (Browser A and Browser B).

  7. Use the glassfish command line to undeploy the application.

    app_dir> asadmin undeploy <app_name>

  8. Use the target clean to remove the temporary directories like /target.

    app_dir> mvn clean

Building, Deploying, and Running the Application in NetBeans IDE

Perform the following steps to build, deploy, and run the application using NetBeans IDE:

  1. Refer to the common build instructions for setting up NetBeans IDE and Java EE 7 SDK.
  2. In the NetBeans IDE, choose File → Open Project (Ctrl-Shift-O), navigate to the samples_install_dir/servlet/ directory, select async-request-war, and click Open Project.
  3. In the Projects tab, right click async-request-war and select Run to build, deploy, and run the project.

Troubleshooting

If you have problems when running the application, refer the troubleshooting document.

 

Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.