You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Since a servlet is blocking, a normal HTTP communication makes the thread wait. So there is a possibility that all the threads in the pool can be used up. By using the asynchronous http client framework, the thread can be released back into the pool and be made available for other purposes until the answer comes back. When the answer comes back, a callback is fired which then relays the response back to the client.

The async http client is to be used in conjunction with webcontainers that are configured for non blocking communication. CometProcesor with Tomcat and Continuation with Jetty are some good examples. This example uses Tomcat.

About this example

This example seeks to demonstrate the usage of the async http client by first configuring the Geronimo server. It then deploys an app which we shall call http-local-app on this configured server.  Another webapp call http-remote-app is deployed on any other server. The servlet on the http-local-app delegates requests to the async http client and returns immediately. The async http client connects to the remote app and when it recieves a response, a callback is fired which relays the message back to the original response stream.

Configuring the server

Install a geronimo server with tomcat webcontainer and start it. Log onto the console by using system/manager as your credentials. Click on the "Web Server" link on the left hand navigation frame. Under "Add new:" section, click on "Tomcat NIO HTTP connector".  Specify a uniquename for the connector and take all the default values. Save this configuration. Your new connector must be listed in the Network Listeners.

Now delete the TomcatWebConnector in the Network Listeners list so that all connections are made to the new Tomcat NIO HTTP connector.

The http-local-app

The AsyncServlet in this app implements the CometProcessor interface. This will make it's event method invoked rather than the usual service method, according to the event which occurred. The event object gives access to the usual request and response objects, which may be used in the usual way. More information about this can be read here.

The servlet also generates a random delay time and a random http status code. Using the async client, it connects to a remote url and passes these randomly generated values to it. The remote url is initialized from the servlet's <init-param> value.  Once the async client sends a request to the remote app, the servlet returns without waiting for a response from the remote app.  This app has a callback listener which holds the CometEvent object. When it receives the answer from the remote app, it gets the original response from the event object and passes the message to it.

Deploy this app on the Geronimo server we configured earlier using either the consoleor the command line

$geronimo_home/bin>./deploy.sh deploy http-local-app-2.0-SNAPSHOT.war

The http-remote-app

A simple servlet in this app reads the two request parameters delay and code.  It sleeps for the amount of time specified in the delay. If the code is 200, it serves a file called dummy.html. For the status code 500, it does a divide by zero to throw a servlet exception. For other status codes, it sets the appropriate response code in the response.sendError(). It then returns. This simulates varous response times and return status codes from an external app. This app can be run on any other server and machine.

  • No labels