HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

HTTP Session Garbage Collector
Removing cached data

A common approach to caching data in Web applications is to use an HTTP session. A business use case that spans multiple HTTP requests may create the need for caching in a Web tier. Once business use–case processing is completed, this cached data needs to be removed. Failure to do this may lead to memory leakage, which becomes noticeable when a user HTTP session continues for hours.

The Solution
The following terminology is used in subsequent sections:

  • A use case is a sequence of steps performed by a user toward realization of a business requirement.
  • A nested use case is the extension point from the base use case.
  • The use case context represents the logical starting point of a use case.
  • The handler is a server-side component responsible for processing HTTP requests.
  • The cache element is an object, cached during use case processing.
  • The navigation path is the sequence of handlers invoked when fulfilling a use case.
Let's begin by considering a few basic rules.

Rule 1: Only one use case is active at any instance: A user cannot process multiple use cases at a given instance.

Rule 2: A use case processing stage governs the cache element life cycle:

  1. Start stage: New use case processing starts on completion or termination of prior use case processing. Cache elements that correspond to a prior use case are removed.
  2. Intermediate stage: The user navigates to the next step or to a prior step of the use case. Forward navigation adds new cache elements; backward navigation may remove cache elements on an as-needed basis.
  3. End stage: On completion of the use case, all accumulated cache elements are removed.
To implement this rule we need to provide an additional parameter to identify the use case processing stage.

Rule 3: Handlers operate inside a use case context: User navigation dynamically builds a cache hierarchy tree with the handler and use case context as nodes and the cache element as a leaf. The handler node could hold a successor handler, cache elements, and a nested use case context. The use case context node holds a start handler. The root of the tree is the use case context.

Rule 4: Handlers operate on cache elements: Handlers create, read, and destroy cache elements.

Keeping these rules in mind, let's address the stated problem. We have conceptually built a cache hierarchy tree, which is the key concept to addressing the problem.

Step 1: Each request will go through the cache garbage collector
At the start of a new use case, the cache garbage collector removes all existing cache elements of the previous use case.

Figure 1 addresses the memory leakage problem. The introduction of the cache hierarchy tree removes tight coupling between handlers.

Figure 1

Step 2: Handlers navigate the cache hierarchy tree
A pointer is maintained to navigate the cache hierarchy tree. It points to the most recently processed node in the use case context. With the help of this pointer we can:

  • Jump to a previous step
  • Jump the nested use case context
During a jump operation all cache elements that are attached to downstream handlers may be removed. It's helpful in the following business scenarios: backward navigation and use case termination.

Efficient memory handling at a granular level is achieved. At the same time, the decoupled handlers make future enhancements easier.

Solution Implementation
Cache Hierarchy Tree Design
The cache hierarchy tree is built using the GoF Composite pattern (see Figure 2). The use case context and handlers are the nodes; the cache element names are leaves; the actual cache element instances reside in the HttpSession.

Figure 2

Cache Hierarchy Tree Management
The "UserAppStateContainer" object holds the cache hierarchy tree. This container instance is stored in the user is HTTP session. "UserAppStateManager" helps manage the cache hierarchy tree.

Conclusion
The cache hierarchy tree empowers HTTP session garbage collection; however, this tree is also kept in the user HTTP session. So in a clustered environment it also needs to be replicated.

About the Author
Abhinasha Karana is a technical specialist for the systems integration group at Infosys Technologies. He has around six years of experience in the information technology field as a developer and designer. His interests are in EAI and distributed computing. [email protected]

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.