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:

- Asynchronous processing in servlets. The server thread does not have to wait until the response is ready and can be reused by the container.
- Asynchronous processing listeners in servlets.

## 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:

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`

5. Open two different browser windows to run this application (Browser A and Browser B).
   - In Browser A, open `http://<javaee.server.name>:<javaee.server.port>/async-request-war`
     Repeat this step for Browser B.
   - In Browser A, enter a user name in the text box; for example, `userA`. 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.

   - Now, you are ready to chat. In Browser A, type "Hello" in the text box and click the `Post Message` button. The following message appears in both Browser A and Browser B:

     `userA:
     Hello`

     Try the same step in Browser B.

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

   `app_dir> asadmin undeploy` <app_name>

7. 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.