Silent Warrior
Silent Warrior

Reputation: 5265

How to clean session attribute from all active session in java?

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 ?

Upvotes: 0

Views: 3210

Answers (2)

Gennady Shumakher
Gennady Shumakher

Reputation: 5746

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.

Upvotes: 1

Ryan Fernandes
Ryan Fernandes

Reputation: 8526

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

Upvotes: 4

Related Questions