Salman A. Kagzi
Salman A. Kagzi

Reputation: 4053

How to update multiple sessions based on an Event

In our web application (in JBoss using Struts) we use sessions largely for security as well as to cache some data for a User. Thus, every user logged into the application has a session and different data cached in it.

Based on some parameter change, i want to change the cache of the subset of users who are logged in (i.e. have session)

Can this be achieved? I have not been able to find anything so far from general search.

Upvotes: 0

Views: 276

Answers (3)

TheWhiteRabbit
TheWhiteRabbit

Reputation: 15758

You can use a HttpAttributeListener

a basic example here

HttpSessionAttributeListener:

The HttpSessionAttributeListener interface enables an object to monitor changes to the attribute lists of sessions within a given Web application. The HttpSessionAttributeListener in turn extends java.util.EventListener. The methods in it are

  • attributeAdded(HttpSessionBindingEvent se)- This is the notification that an attribute has been added to a session.
  • attributeRemoved(HttpSessionBindingEvent se)- This is the notification that an attribute has been removed from a session.
  • attributeReplaced(HttpSessionBindingEvent se)- This is the notification that an attribute has been replaced in a session.

Upvotes: 1

BalusC
BalusC

Reputation: 1108722

You have basically 2 options:

  1. Push the changes. Get hold of all HttpSession instances in an application wide map which you manage with help of a HttpSessionListener. This way you can just get them from the application scope and walk through them to make the necessary changes directly.

  2. Poll the changes. Store a change instruction in the application scope. On every HTTP request, check with help of a Filter or ServletRequestListener if a change is required, then make the necessary change in the current session and remove/disable the change instruction.

A completely different alternative is to use an application wide shared cache, such as Terracotta or Ehcache, so that you don't need to duplicate the same data over all HTTP sessions. You'd just need to deal with the data on a per-request basis. When database access comes into picture with JPA, then read on about "2nd level cache", that's exactly what it does.

Upvotes: 0

Bozho
Bozho

Reputation: 597096

You can do it by storing each session object in a static List<Session> in some holder object. You can put it by a HttpSessionListener#sessionCreated(..). Remember to remove it from the list on sessionDestroyed(..)

Then, whenever you want to do something, simply loop the previously stored list of sessions and do whatever you want with them.

Upvotes: 1

Related Questions