Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

...

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.

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.

Configuring the server

Using console

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.

Editing the config.xml

  1. Stop the server.
  2. edit geronimo_home/var/config/config.xml
  3. add load="false" to the TomcatWebConnector gbean. The line would now look like this
    Code Block
    
    <gbean load="false" name="TomcatWebConnector">
    

...

  1. for the module org.apache.geronimo.configs/tomcat6/2.1-SNAPSHOT/car,

...

  1. add

...

  1. the

...

  1. following

...

  1. gbean

...

  1. Code Block

...

  1. 
    <gbean gbeanInfo="org.apache.geronimo.tomcat.connector.Http11NIOConnectorGBean" name="org.apache.geronimo.configs/tomcat6/2.0.1/car?ServiceModule=org.apache.geronimo.configs/tomcat6/2.0.1/car,j2eeType=GBean,name=TomcatNIOConnector">
    <attribute name="useExecutor">true</attribute>
    <attribute name="maxThreads">40</attribute>
    <attribute name="pollerThreadCount">1</attribute>
    <attribute name="socket_soTimeout">5000</attribute>
    <attribute name="selectorTimeout">1000</attribute>
    <attribute name="connectionTimeout">60000</attribute>
    <attribute name="socket_soLingerOn">true</attribute>
    <attribute name="acceptorThreadCount">1</attribute>
    <attribute name="port">8080</attribute>
    <attribute name="pollerThreadPriority">5</attribute>
    <attribute name="compressableMimeType">text/html,text/xml,text/plain</attribute>
    <attribute name="socket_performanceLatency">0</attribute>
    <attribute name="useComet">true</attribute>
    <attribute name="useIPVHosts">false</attribute>
    <attribute name="maxHttpHeaderSize">4096</attribute>
    <attribute name="restrictedUserAgents" value=""/>
    <attribute name="acceptorThreadPriority">5</attribute>
    <attribute name="proxyPort">0</attribute>
    <attribute name="maxPostSize">2097152</attribute>
    <attribute name="minSpareThreads">10</attribute>
    <attribute name="socketBuffer">9000</attribute>
    <attribute name="redirectPort">8443</attribute>
    <attribute name="bufferSize">2048</attribute>
    <attribute name="socket_soReuseAddress">false</attribute>
    <attribute name="useSendfile">true</attribute>
    <attribute name="threadPriority">5</attribute>
    <attribute name="socket_tcpNoDelay">false</attribute>
    <attribute name="useBodyEncodingForURI">false</attribute>
    <attribute name="maxSpareThreads">100</attribute>
    <attribute name="enableLookups">true</attribute>
    <attribute name="socket_performanceConnectionTime">1</attribute>
    <attribute name="socket_appWriteBufSize">8192</attribute>
    <attribute name="maxKeepAliveRequests">100</attribute>
    <attribute name="socket_ooBInline">true</attribute>
    <attribute name="socket_keyCache">500</attribute>
    <attribute name="disableUploadTimeout">true</attribute>
    <attribute name="socket_bufferPoolSize">104857600</attribute>
    <attribute name="keepAliveTimeout">60000</attribute>
    <attribute name="acceptCount">10</attribute>
    <attribute name="socket_txBufSize">43800</attribute>
    <attribute name="connectionLinger">-1</attribute>
    <attribute name="command_line_options">true</attribute>
    <attribute name="tcpNoDelay">true</attribute>
    <attribute name="oomParachute">1048576</attribute>
    <attribute name="socket_appReadBufSize">8192</attribute>
    <attribute name="selectorPool_maxSpareSelectors">-1</attribute>
    <attribute name="xpoweredBy">false</attribute>
    <attribute name="socket_rxBufSize">25188</attribute>
    <attribute name="maxSavePostSize">4096</attribute>
    <attribute name="socket_soLingerTime">25</attribute>
    <attribute name="compression">off</attribute>
    <attribute name="host">0.0.0.0</attribute>
    <attribute name="socket_eventCache">500</attribute>
    <attribute name="socket_performanceBandwidth">1</attribute>
    <attribute name="noCompressionUserAgents" value=""/>
    <attribute name="name">TomcatNIOConnector</attribute>
    <attribute name="socket_soKeepAlive">false</attribute>
    <attribute name="socket_soTrafficClass">28</attribute>
    <attribute name="socket_directBuffer">false</attribute>
    <attribute name="selectorPool_maxSelectors">200</attribute>
    <attribute name="emptySessionPath">false</attribute>
    <attribute name="uriEncoding">ISO-8859-1</attribute>
    <attribute name="allowTrace">false</attribute>
    <attribute name="processCache">200</attribute>
    <attribute name="socket_processorCache">500</attribute>
    <attribute name="server" null="true"/>
    <attribute name="proxyName" null="true"/>
    <reference name="TomcatContainer">
    <pattern>
    <groupId>org.apache.geronimo.configs</groupId>
    <artifactId>tomcat6</artifactId>
    <version>2.0.1</version>
    <type>car</type>
    <name>TomcatWebContainer</name>
    </pattern>
    </reference>
    <reference name="ServerInfo">
    <pattern>
    <groupId>org.apache.geronimo.configs</groupId>
    <artifactId>j2ee-system</artifactId>
    <version>2.0.1</version>
    <type>car</type>
    <name>ServerInfo</name>
    </pattern>
    </reference>
    </gbean>
    

...

  1. The gbean uses version 2.0.1

...

  1. .

...

  1. Replace

...

  1. that

...

  1. with

...

  1. the

...

  1. appropriate

...

  1. version

...

  1. of

...

  1. your

...

  1. server

...

  1. Start

...

  1. the

...

  1. server.

...

Building the sample

  1. Download the zip file which contains both the apps discussed above.
  2. Unzip the file into a directory of your choice, say samples.
  3. cd into samples\async-http directory\http-local-app\src\main\webapp\WEB-INF

...

  1. directory.

...

  1. edit

...

  1. the

...

  1. web.xml

...

  1. there

...

  1. and

...

  1. change

...

  1. the

...

  1. value

...

  1. of

...

  1. the

...

  1. <remoteUrl>

...

  1. initi-param.

...

  1. This

...

  1. should

...

  1. be

...

  1. set

...

  1. to

...

  1. the

...

  1. machine

...

  1. where

...

  1. your

...

  1. remote

...

  1. app

...

  1. will

...

  1. be

...

  1. deployed.

...

  1. cd

...

  1. back

...

  1. to

...

  1. async-http

...

  1. directory.

...

  1. execute

...

  1. mvn.

...

  1. This

...

  1. will

...

  1. build

...

  1. the

...

  1. 2

...

  1. WARs.

...

Running

...

the

...

sample

...

  1. Deploy samples\async-http\http-local-app\target\http-local-app-2.0-SNAPSHOT.war

...

  1. on

...

  1. the

...

  1. Geronimo

...

  1. server

...

  1. we

...

  1. configured

...

  1. earlier

...

  1. using

...

  1. either

...

  1. the

...

  1. console

...

  1. or

...

  1. the

...

  1. command

...

  1. line

...

  1. Code Block

...

  1. 
    $geronimo_home/bin>./deploy.sh deploy _samples_\async-http\http-local-app\target\http-local-app-2.0-SNAPSHOT.war
    

...

  1. Deploy samples\async-http

...

  1. directory\http-remote-app\target\http-remote-app-2.0-SNAPSHOT.war

...

  1. on

...

  1. any

...

  1. server

...

  1. on

...

  1. the

...

  1. remote

...

  1. machine.

...

  1. Point

...

  1. your

...

  1. browser

...

  1. to

...

  1. http://localhost:8080/localApp/async

...

  1. .

...

  1. If

...

  1. you

...

  1. wish

...

  1. to

...

  1. override

...

  1. the

...

  1. remoteUrl

...

  1. set

...

  1. in

...

  1. the

...

  1. <init-param>

...

  1. of

...

  1. the

...

  1. web.xml,

...

  1. you

...

  1. may

...

  1. pass

...

  1. it

...

  1. as

...

  1. a

...

  1. request

...

  1. param

...

  1. (

...

  1. http://localhost:8080/localApp/async

...

  1. ?remoteUrl=

...

  1. http://foo:8080

...

  1. )

...

  1. You

...

  1. should

...

  1. see

...

  1. either

...

  1. a

...

  1. dummy

...

  1. html

...

  1. or

...

  1. one

...

  1. of

...

  1. the

...

  1. popular

...

  1. HTTP

...

  1. errors.