Session management
As mentioned in my first article, the DVDService application is a
single-user application since there is only one set of DVDs
maintained on the server side, and this set is shared by all requests
to the server. Having only one set of DVDs may seem odd since every
request to the DVDService service as default creates a new instance
of the DVDService class. The explanation is that the set of
DVDs is held as static data (in a single instance of the
DVDManager class):
private static DVDManager dm = initialize();
The dm variable is therefore used by all instances of the
DVDService class, and hence used by all users. As long as
it’s your private DVD collection and you’re the only one
maintaining it, then this solution is fine. The situation is quite
different if many users are simultaneously allowed to maintain the
same set of DVDs. What you’d like to have is a mechanism that
allocates a unique set of DVDs for each user. Luckily enough, Axis
provides such a feature.
Actually Axis gives us three possibilities:
- "request" scope (default): create a new instance of the service
object for each request.
- "session" scope: create a new instance for each client accessing
the service
- "application" scope: share one "singleton" instance between all
clients and all requests.
The scope must be defined in the wsdl file:
<service name="MyService"...>
<parameter name="scope" value="value"/>
...
</service>
|
value must be request, session, or
application.
In order to establish session scope the client must also specify
this option:
. . .
call = (Call) service.createCall();
call.setMaintainSession(true);
. . .
|
Having made these modifications the DVDs should no longer be held
in static data, so we change the previous line to:
private DVDManager dm = initialize();
If we use the tcpmon utility to show the SOAP traffic on
the wire, we can see that Axis implements session scope using
cookies. When the first request is sent from the client, the HTTP
response contains a session-ID:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Date: Mon, 30 Dec 2002 07:08:34 GMT
Server: Apache Tomcat/4.0.1 (HTTP/1.1 Connector)
Connection: close
Set-Cookie: JSESSIONID=64035478A4F085F830B241F0E274C508;Path=/axis
|
This session-ID will be sent by Axis on all subsequent
requests--if you have specified setMaintainSession(true).
Here’s a request that uses the above session-ID:
POST /axis/services/DVDService HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.0
Host: localhost
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Content-Length: 401
Cookie: JSESSIONID=64035478A4F085F830B241F0E274C508
|
To verify that things work as they should, a couple of lines are
printed to System.out from the DVDService class. Every
time the class is instantiated and the DVDs are read from the
XML-file, a message like this appears:
DVDService being initialized...
DVDService: Number of DVDs found: 3
|
If we now start two clients—using for example the
DVDClient presented in the first article—we’ll see
that the two lines are printed when each client starts, and if we use
the functions in the clients no further lines are printed. This
proves that we have two and only two instances of the
DVDService class on the server.
[Previous]
[Next]
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.