Farrukh is from Pakistan and works in Qatar for a diplomatic company as Senior System Analyst. His expertise are Java EE and Oracle SQL, PL/SQL. His hobbies are Open Source development, driving/outing/long-ride, dining and watching movies over the weekend. Farrukh has posted 4 posts at DZone. View Full User Profile

Invalidating Orphaned Sessions using Vroom Web Framework

08.20.2008
| 4241 views |
  • submit to reddit

Those who've been using the Vroom Web Framework for some time should be familiar with the areas that Vroom focuces on. Things that are not so simple or time consuming are just a piece of cake, doable with in few minutes. (Interview with author of framework here.)

Orphaned Session: Let's say I log on to Yahoo Mail and do not do anything for some time, my session is considered inactive and expires after the specified timeout by the Yahoo mail application. What if I log on and find that there is no new mail and instead of logging out, I simply go to a different url or close the browser. What happend to my session? It can be considered as inactive but the thing is that it's no longer in use and will be invalidated after the session timeout occurs. I consider these type of sessions to be orphaned.

With Vroom Web Framework, the following is just fine to take care of orphaned sessions, no matter at which stage you closed the browser or opened a different URL.

Define a java class called "com.myco.myapp.bean.SessionBean" and add the following code:

public class SessionBean {

Integer tracker = 0;

public void onLoad() {
synchronized(tracker) {
tracker++;
}
}

public void onUnload(HttpServletRequest req) {
synchronized(tracker) {
tracker--;
}
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
// Log exception...
} finally {
if(tracker == 0) {
req.getSession().invalidate();
}
}
}

}

In this class, we've defined one variable named "tracker" and two methods "onLoad" and "onUnload". onLoad method increases the tracker value by 1 and onUnload decreases it by 1 and sleeps for 10 seconds. After 10 seconds, if the tracker value remains 0, the session is treated as orphaned and invalidated.

The next step is to bind these methods with every html, jsp, do, faces page depending on your framework. Define the following rule in vroom-config.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<vroom-config>
<webpage uri="/|(/|[a-zA-Z0-9]|-|_)*.(jsp|html|do|faces)">
<object name="document" bean-class="com.myco.myapp.bean.SessionBean"
var="sb" scope="session">
<event type="onload" method="onLoad"/>
<event type="onunload" method="onUnload"/>
</object>
</webpage>
</vroom-config>

In the rule, just note that we've bound the onload and onunload events of document object with SessionBean's onLoad and onUnload methods.

Let's say we've page1.jsp, page2.jsp and page3.jsp in our application. When the user opens page1.jsp, the value of tracker becomes 1 because onLoad method gets called. Now the user opens page2.jsp on a different tab, the value of tracker becomes 2 because tabs in browsers usually share a single session. If user loads page3.jsp in tab which was displaying page1.jsp, onUnload method gets called because page1.jsp is going to unload so the tracker value becomes 1 and the thread sleeps for 10 seconds. Meanwhile page3.jsp is loaded and tracker again becomes 2. When the thread wakes up the value of tracker remains 2 so session remains valid. Now user closes the browser and onUnload method gets called for both page2.jsp and page3.jsp. The tracker value becomes 0. After 10 seconds when the thread wakes up, the value of tracker remains 0 so the session is invalidated.

This helps web application not to have orphaned sessions at all and to make the application more efficient and resource optimized.

The nice thing is that even if you don't want to use all of the features Vroom Web Framework provides, you can still get benefit of the features you like by adding support of Vroom Web Framework to your existing applications without any changes in your application architecture.

0
Your rating: None
Published at DZone with permission of its author, Farrukh Ijaz.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)