views:

639

answers:

2

Currently I am working on web project which uses JSP/Servlet and struts framework. We are using cache mechanism. I want to clean some of the session attribute from all the active sessions on particular event (For e.g. in case of refreshing cache). So what is best way to implement same ?

+4  A: 

Let me guess.. you're storing the value from the cache into a session variable, right?

The cleanest way to do this is to retrieve the value from the cache every time you need it and let the cache manage the expiry/reload etc. That is the responsibility of the cache.

In other words, don't store the value from the cache into the session objects. It will serve no purpose when you're using a cache.

Cheers

Ryan Fernandes
A: 

You need to provide a class which implements HttpSessionActivationListener (part of server api) interface and register it in web.xml.

Then you can track the active sessions and use that information to get access to the sessions and perform an update of the attribute. Effectively the implementation class will be a singleton, so you have to treat carefully the synchronization issues during sessions tracking.

Of course if you have a cluster environment with multiple nodes the propagation of attribute change may be quite complex.

Gennady Shumakher