I need to write a servlet that, when called, gets information about a list of the currently opened sessions.
Is there a way to do this?
I need to write a servlet that, when called, gets information about a list of the currently opened sessions.
Is there a way to do this?
Perhaps using a JMX bean is more elegant and needs no code. Just read the value of
data: jboss.web:type=Manager,path=/myapplication,host=localhost" activeSessions
Implement
HttpSessionListener
, give it astatic Set<HttpSession>
property, add the session to it duringsessionCreated()
method, remove the session from it duringsessionDestroyed()
method, register the listener as<listener>
inweb.xml
. Now you've a class which has all open sessions in the current JBoss instance collected. Here's a basic example:Then in your servlet just do:
If you rather want to store/get it in the application scope so that you can make the
Set<HttpSession>
non-static, then let theHttpSessionCollector
implementServletContextListener
as well and add basically the following methods:which you can use in Servlet as follows: