Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Manage distributed sessions - JavaWorld April 2001

Manage distributed sessions - JavaWorld April 2001

Tutorial Details:

Manage distributed sessions
Manage distributed sessions
By: By Kelly Davis and Robert Di Marco
To avoid a single point of failure, use a distributed architecture for managing sessions
or a good description of what sessions are and what the problem with having sessions across multiple servlet servers is, refer to Thomas E. Davis and Craig Walker's "Take Control of the Servlet Environment, Part 2: Alternatives to Servlet Session Management" ( JavaWorld, December 21, 2000). Basically, the problem is that if you have more than one servlet server, session information exists only in one servlet engine's JVM and is not communicated to the other servlet servers. If a servlet server fails or shuts down for maintenance, any information saved in a session is lost. Also, if an environment has multiple servlet servers, a user with a session must always be redirected to the same servlet server in order to reference any data in that session. Davis and Walker suggest using a relational database that all servlet servers can access to save the session information. However, that solution still has a single point of failure: the server hosting the database. If the database went down, all session information would become unavailable to the servlet servers. Also, saving serialized objects into the database is a functionality that is difficult to implement in all databases.
Another possibility for multiserver session management utilizes the JavaSpaces API (for documentation, see Resources ) to maintain records of the session objects. However, if the machine hosting the JavaSpace were to go down because of crashing or maintenance, all session information would be lost. So again, we are left with a single point of failure.
To accomplish the distributed session server with n-nodes, we must address three main problems:
How to set up a repository to store session information
How to synchronize distributed repositories
How to let a servlet server retrieve the session information in such a way that if one repository suddenly goes offline, it tries to retrieve the object from the next repository
Introduction to the Mnemosyne
The repository we will use to store the session information is an implementation of the Mnemosyne interface (named after the Greek goddess of memory and mother of the muses). An object that implements Mnemosyne is responsible for managing all objects in the repository. Any other object that wants to write, retrieve, or remove objects in the repository must call a method of the Mnemosyne .
To be saved to a Mnemosyne , an object must implement the Memory interface, which defines the equalsMemory() operation for determining whether two memory objects are equivalent. That allows the Mnemosyne to figure out what object should be returned on a read or a take request. The Memory interface also extends Serializable so that you can use RMI to transmit the object across the network.
A Mnemosyne uses three other interfaces to represent its state:
A CommonContext interface will store all information for the Mnemosyne . Each Mnemosyne will have one instance of a CommonContext object to allow for synchronization between methods when reading, writing, and taking Memory objects. For writing and taking, the CommonContext defines both "silent" and "loud" methods. The silent methods are used when objects need to be added without any event notifications. For example, when a Mnemosyne receives a WriteRemoteEvent (a notification that an object has been written to a remote Mnemosyne ), it will want to write another object to the CommonContext . However, it doesn't need to notify the other remote Mnemosyne s; the original Mnemosyne has notified them. So the write is done "silently" by calling the CommonContext 's silentWrite() method. The "loud" methods give details of the event to any interested listeners that are called when an object is first put into the context.
A Transaction interface allows for distributed transactions when reading, writing, or taking Memory objects. That means multistep operations can occur on the Mnemosyne .
A TransactionContext interface manages a distributed transaction. That makes it possible to abort or commit transactions.
Keeping Mnemosyne s synchronized is accomplished with two methods defined by the Mnemosyne : synchronize() and notify() . synchronize() is intended to get a local Mnemosyne "in sync" with a Vector of other Mnemosyne s. (Those Mnemosyne s may be local or remote, but for the sake of clarity, we will assume they are remote.) A sample implementation of the synchronize() method (from the MnemosyneImpl class) is displayed below. (See Resources for the complete sample code to this article.)
public void synchronize(Vector Mnemosynes)
throws RemoteException, TransactionException
{
// ...
// The MatchAllMemory object is a utility class that
returns true when
// compared against any Memory object
MatchAllMemory matchAllMemory = new MatchAllMemory();
// Obtain all Memory's from the Primary
Mnemosyne Mnemosyne = (Mnemosyne) Mnemosynes.firstElement();
Vector allMemories = Mnemosyne.readAll(matchAllMemory,null);
// Write all Memory's silently as there is no need
// to notify write listeners
commonContext.silentWriteAll(allMemories);
// Register to send and receive events
Enumeration enumeration = Mnemosynes.elements();
while(enumeration.hasMoreElements())
{
Mnemosyne nextMnemosyne = (Mnemosyne) enumeration.nextElement();
// Register to obtain notification
nextMnemosyne.addTakeRemoteEventListener(this, matchAllMemory);
nextMnemosyne.addWriteRemoteEventListener(this, matchAllMemory);
// Register to send notification
addTakeRemoteEventListener(nextMnemosyne, matchAllMemory);
addWriteRemoteEventListener(nextMnemosyne, matchAllMemory);
}
// ...
}
The local Mnemosyne object reads all of its Memory objects of the first Mnemosyne in the Vector , and silently writes them to its CommonContext object. Next, the local Mnemosyne adds itself to all remote Mnemosyne s, as both a TakeRemoteEventListener and a WriteRemoteListener . That means that any takes or reads on the remote Mnemosyne s will result in a call to the local Mnemosyne 's notify() method. Finally, the local Mnemosyne adds the remote Mnemosyne to its list of TakeRemoteEventListener s and WriteRemoteListener s. This ensures that any write or take calls will notify the remote Mnemosyne .
Now our synchronized local Mnemosyne object needs to update all other Mnemosyne s whenever a Memory object is added or removed. You can accomplish that with the notify() method. Whenever a write or take event occurs, a Mnemosyne calls the notify() method of all appropriate listeners for the event. In the synchronize() method, you register the local Mnemosyne as a listener for take and write events on all remote Mnemosyne s. If such an event occurs on any of those remote Mnemosyne s, the local Mnemosyne 's notify() method will be called. When that happens, the local Mnemosyne must respond to the event. Below is an example of how the Mnemosyne can synchronize itself with the remote Mnemosyne :
public void notify(RemoteEvent remoteEvent) throws RemoteException
{
// Write the written Memory to self, but no need to notify all of the
// Mnemosynes
if(remoteEvent instanceof WriteRemoteEvent)
{
WriteRemoteEvent wre = (WriteRemoteEvent) remoteEvent;
commonContext.silentWrite(wre.getMemory());
}
// Take the written Memory from self, but no need to notify all of the
// Mnemosynes
if(remoteEvent instanceof TakeRemoteEvent)
{
TakeRemoteEvent tre = (TakeRemoteEvent) remoteEvent;
commonContext.silentTake(tre.getMemory());
}
}
A Mnemosyne has now been set up that can hold memory objects, synchronize itself with remote Mnemosyne s, and keep itself up to date if any remote Mnemosyne s gain or lose a Memory object.
To manage HTTP sessions using Mnemosyne s, a servlet creates an instance of HttpSession (using the getSession() method from HttpServletRequest ), wraps the session in a class that implements Memory (in this example, it is called SessionWrapper ), and writes the wrapper class to a Mnemosyne by calling the Mnemosyne object's write() method.
By calling the write() method, the Memory object that wraps the session is serialized, sent down the network to the Mnemosyne , and instantiated on the remote machine. When the Memory object is written to the Mnemosyne , a WriteRemoteEvent is sent to all WriteRemoteEventListener s registered with the Mnemosyne . This allows all other Mnemosyne s to add the new object to their repository as Mnemosyne s.
To look up a stored session, a servlet calls the read() method to look for the Memory object that contains the session. If the Mnemosyne finds the object, the object is sent via RMI back to the servlet server.
Finally, to remove the session, the servlet would call the Mnemosyne' s take() method. The Mnemosyne would send back the Memory object just like it does on the read, but the object would also be removed from the repository. Also, a TakeRemoteEvent would be sent out to all TakeRemoteEventListener s. That would notify all remote Mnemosyne s of the Memory object's removal.
Setting up a session server
Now that we have shown how the repository of objects will be maintained on multiple servers, we will show you how to build an implementation of a session server. On initialization, a session server does the following:
Creates the local Mnemosyne object
Binds the local Mnemosyne to the RMI registry
Synchronizes the local Mnemosyne object with all other remote Mnemosyne s
First, an instance of the Mnemosyne interface is obtained from the factory and is bound to the local IP for the session server. (Refer to the SessionServer object for a full implementation.)
protected void bindMnemosyne()
{
//...
// Get Mnemosyne
Mnemosyne Mnemosyne = null;
try
{
Mnemosyne = MnemosyneFactory.getMnemosyne();
}
catch(RemoteException remoteException)
{
System.out.println("Internal error:")


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Manage distributed sessions - JavaWorld April 2001

View Tutorial:
Manage distributed sessions - JavaWorld April 2001

Related Tutorials:

Displaying 1 - 50 of about 481 Related Tutorials.

Working with sessions
Working with sessions Working with sessions                    
 
Eclipse Plunging/Team Development
. This integration has been available since January 2003, and on April 9th... of very large distributed projects.       ... to create issues, manage their states, link them to other issues, related
 
MySQL Client
This all-in-one tool will help you create, manage and edit MySQL databases...;  The MySQL liternals Manual This documentation is NOT distributed... in different ways. Some key features: Fully MySQL 5.x compliant. Manage Stored
 
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Developing Distributed application using Enterprise Java Beans, J2EE.... Distributed Architecture     ... distributed transactional applications for the enterprise. Thin-client
 
Open Source Distributed Cache Solutions written in Java

 
How To Manage Your Username And Password The Easy And Secure Way
How To Manage Your Username And Password The Easy And Secure Way How To Manage Your Username And Password... if not daily. I used to use Microsoft Excel to manage my usernames, passwords
 
Java & JEE books Page1
in these 35 tutorial sessions, however, we will cover most of the basics..., remote procedure calls, distributed or network programming, and parallel
 
Java & JEE books
tutorial sessions, however, we will cover most of the basics - statements..., remote procedure calls, distributed or network programming, and parallel... distributed applications. This model is based on well-defined components that can
 
Open Source content Management System
. OpenCms helps to create and manage complex websites easily without knowledge of html... code (the language in which the program is written) is freely distributed...) to manage a Web site used to be a luxury within the reach of only those who had
 
HttpSessionListener
know about the sessions. As we know that Http protocol is a "stateless"... of active sessions in a web application  This interface has  two methods
 
Definition of Bioinformatics
; About Bioinformatics In February 2001, the human genome was finally... for Biotechnology Information (NCBI 2001) defines bioinformatics as: "
 
Extended VS Presentation
powerful editing sessions management and highly customizable and very useful Eclipse skin ("look and feel"). Editing sessions management allows you
 
Use Of Form Bean In JSP
about the procedure of handling sessions by using Java Bean. This section provides
 
XPairtise - Pair Programming for Eclipse
plug-in XPairtise provides a platform for distributed pair programming. The plug... programmers share the same terminal and program together. Distributed pair programming
 
Xcarecrows4 Web Services
to manage Apache Tomcat and Apache Axis2 in a few clicks. Install and configure.... Manage Web Applications Manage Tomcat users Deploy and undeploy Web Services... efficiently. Install, start, stop and restart Apache Tomcat, set Apache SSL, manage
 
HttpSessionListener example
should firstly know about the sessions. As we know that Http protocol is a "... there is a change in the list of active sessions in a web application  This interface
 
httpsessionbindinglistener
of the HttpSessionBindingListener we should firstly know about the sessions. As we know that Http
 
JBoss Administrator
; We are looking for JBoss Administrator to manage the JBoss based
 
httpsessionbindinglistener example
of the HttpSessionBindingListener we should firstly know about the sessions. As we know
 
rPath Linux 0.99.6 (Beta) has been released now
is a Linux distribution built with the new Conary distributed software management..., built with the Conary distributed software management system, is not only
 
JMS Training
developers who want to start using JMS and Message Driven Beans to build distributed
 
Cookie Example to Store and Show only 10 values
how you can manage cookie value in your JSP page. This example is saving
 
JEE-specific Design patterns
the widest range of applications development in the enterprise and distributed... distributed business services, network performances degradation due to multiple
 
SCADA Programming
is a distributed measurement and control system for large-scale industrial automation... are collectively called DCS (Distributed Control System). DCS have conventionally been used
 
Introduction to MySQL
; MySQL is supported, distributed and developed by MySQL AB. It is most
 
What is the use of java?
in the distributed environment on the Internet. Java has a GUI features that  provides you... Object oriented Portable Distributed High... on distributed environment. It is designed to work on distributed computing , Any
 
Open Source Hardware
GIMP is the GNU Image Manipulation Program. It is a freely distributed piece..., modified and distributed. Its license is compatible with the GPL. It runs on all... infrastructure where the distributed efforts of the vision community can be consolidated
 
Common Interview Questions Page - 11
yourself organized? And do you manage your time well? Answer: Here you have... manage your time very well by completing your project before schedule. Also mention
 
Jigloo SWT/Swing GUI Builder
and manage both Swing and SWT GUI classes. Jigloo creates and manages code
 
Nexenta OS Alpha 3 has been released
, and in particular: OpenOffice.org 2.0 (distributed with both InstallCD and LiveCD.... OpenOffice 2.0 (distributed with both InstallCD and LiveCD). 2. Storage Subsystem
 
Open Source CRM
Open Source CRM Open Source CRM Open Source CRM solution Daffodil CRM is a commercial Open Source CRM Solution that helps enterprise businesses to manage customer relationships in a highly organized
 
What is Service-Oriented Architecture?
for the development of loosely coupled distributed applications. In fact service-oriented... distributed services with each other, it enable client to Services communications
 
Resource Bundle Editor
localized properties files. It allows you to manage key/value information
 
SEE with Java
professional to manage ERP application development.  You job
 
Linux Administrator
; We are looking for Linux Administrator to manage Linux and Unix servers
 
Web sphere Portal Analyst
; Job Description  To develop and manage the web portals based
 
JSF Interview Questions
. These are objects that manage interaction with a user. Components help
 
Maven 2: Features
development lifecycle management. Maven was originally developed to manage
 
Objective-c memory management: retain and release
how to manage memory with language Objective-C. Programmer can allocate memory
 
Open Source MMORPG
Open source MMORPG Open Source MMORPG Open Source MMORPG In the article, one of the fellas in World Forge apparently mentioned that they're not looking to manage the tremendous load of existing, commercial
 
Maven2 Tutorial
with application development lifecycle management. Maven was originally developed to manage
 
SME Server 7.0 Pre 2 has been released now
, the e-smith server and gateway, in April 1999. By the end of the year, many... a solid, easy-to-use server for their small-business customers. In July 2001, e-smith
 
nqDocumentation
shall be distributed among a team and existing fragments linked into one document
 
Persistence O/R Mapping Plug-Ins For Eclipse
? DataXtend? CE provides a distributed, persistent data infrastructure
 
Displaying Hello using RMI
serves as a basic technique for supporting distributed objects in java. The steps
 
Softabar Password Manager
;    Softabar Password Manager is an easy way to manage... be stored to server storage. Features: Simple way to manage your
 
Necessitas - Jar management for Eclipse
a class-path container to manage a projects jars using the ivy dependency manager
 
MySQL Manual
Reference Manual This documentation is NOT distributed under a GPL license. Use
 
We are providing Downloadable Version of K12LTSP Linux
the free downloadable version of K12LTSP 4.1.0, which is distributed under GNU... packages. It's easy to install and configure. It's distributed under the GNU
 
We are providing Linux CD's for free.
We are providing Linux CD's for free. Mandrake 9.2 Linux Now Available Linux Mandrake 9.2 CD's We are providing the free downloadable version of Mandrake Linux 9.2, which is distributed under GNU public
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.