And some listener examples as well
http://www.java2s.com/Code
Regards,
Tomas Helgi
Main Topics
Browse All TopicsHi JBoss/Java Experts,
This is a somewhat long post, for which I apologise, but I want to ensure I have included all pertinent information.
I would like to reliably monitor (and report on) the number of active sessions for a particular web application deployed on JBoss 4.0.5 (the server is Windows Server 2003).
Initially I looked around for monitoring tools (eg: Hyperic) which would do all this for me, but alas, all of them seemed to monitor everything EXCEPT active sessions for a particular web app. But I did find one piece of software (AdventNet Application Manager) which does the next best thing (for my purposes) - it will execute a script and monitor the output of the script.
For example, say I have a script called sessions.bat, and when it is executed it will (somehow) output the number of sessions to a log file, sessions.log . Sample contents of the log file might be:
active_sessions=35
session_count=672
max_active_sessions=82
rejected_session_creations
expired_sessions=637
longest_session_alive_time
average_session_alive_time
AdventNet Application Manager will execute the script at a desired interval (say every 15 minutes), and then read the contents of the resulting log file. I can even specify that it needs to look for specific strings (eg: "active_sessions"), and monitor the corresponding numeric values (it even creates pretty graphs for me!).
BUT - I need a script which will output the number of active sessions.
I have done a bunch of searching, and posted on other forums, and so far the consensus seems to be I need to write my own class which implements HttpSessionListener, and have a couple of methods to monitor when sessions are created and destroyed, and tally the session count somehow. I'm a pretty mediocre Java programmer, so I hunted around for some code which kind of seems to do the trick, but in my testing I found that the session count wasn't accurate (for details see my post at http://forums.sun.com/thre
One person suggested I have a look at the "JBoss Management Console" (http://my-server:8180/web
"...then in the console function tree find: Monitoring - > Web Status -> Full Status.
You will get a page with loads of stats on the right side.
Find [Application list] and link to your application, simply click on it
and at the top of the page you will see all the stats that you need..."
I had a look at this and it looks perfect - it gives me more information than I was expecting, like Active sessions, Session count, Max active sessions, Rejected session creations, Expired sessions, Longest session alive time, and Average session alive time.
But my question is this: how does JBoss get this info when viewing the Management Console? I've been told by someone who replied to my post that this information is not stored in a log file (or anywhere). If it isn't stored, presumably there is an API of some kind which is being called/invoked by the Management Console to gather/retrieve that info. Or do I have it completely wrong? What is interesting to me is that JBoss can get this information without me having to write a custom class. How does it do this?
My preferred solution is to have a batch script (eg: sessions.bat) which calls some API(s) (or a custom class) which will return the current values for those different metrics (Active sessions , Session count , Max active sessions, Rejected session creations, Expired sessions , Longest session alive time , Average session alive time)? Or can they only be viewed via the Management Console? If the batch script can't get the values directly, can I get this information in a class that I write (or "borrow")?
In my ideal world, the batch script would look something like this:
@echo off
REM **************************
REM * APPLICATION SESSION INFO *
REM * *
REM * This script retrieves the session info *
REM * for MyApp and writes it to a log file. *
REM * *
REM **************************
set LOG_FILE=C:\logs\sessions.
REM Initialize the variables
set ACTIVE_SESSIONS=0
set SESSION_COUNT=0
set MAX_ACTIVE_SESSIONS=0
set REJECTED_SESSION_CREATIONS
set EXPIRED_SESSIONS=0
set LONGEST_SESSION_ALIVE_TIME
set AVERAGE_SESSION_ALIVE_TIME
set ACTIVE_SESSIONS=[some magic code to get the info]
set SESSION_COUNT=[some magic code to get the info]
set MAX_ACTIVE_SESSIONS=[some magic code to get the info]
set REJECTED_SESSION_CREATIONS
set EXPIRED_SESSIONS=[some magic code to get the info]
set LONGEST_SESSION_ALIVE_TIME
set AVERAGE_SESSION_ALIVE_TIME
echo active_sessions=%ACTIVE_SE
echo session_count=%SESSION_COU
echo max_active_sessions=%MAX_A
echo rejected_session_creations
echo expired_sessions=%EXPIRED_
echo longest_session_alive_time
echo average_session_alive_time
This log file can then be parsed by the monitoring software, and it will produce pretty graphs and reports for my customers. :)
The bit that is missing is [some magic code to get the info]. Can someone suggest how I might do this?
Please, please, someone tell me it is possible to get this information!
Cheers,
Paul Hobbs
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
And some listener examples as well
http://www.java2s.com/Code
Regards,
Tomas Helgi
I agree with Tomas. Also look at this old discussion.
http://www.experts-exchang
Thanks for the links to the various examples of custom classes for counting sessions. However, I am still unsure about a couple of things:
1. Assuming I use one of the sample classes, how do I get the session count values into a batch script so I can output to a simple log file? Can I do something like this in a batch script:
A HttpSessionListener could do steps 1,2,3, and 4. Maybe something like this.
public class UsersListener implements HttpSessionListener {
private Hashtable sessionsTable = null;
private ArrayList aliveTimeList = new ArrayList();
Timer timer = new Timer();
timer.scheduleAtFixedRate(
private int getCount(){
sessionsTable.size();
}
private synchronized ArrayList getCurrentList(){
// TO DO: get list and make a new one
}
public void sessionCreated(HttpSession
String id = event.getSession().getId()
if(sessionsTable == null)sessionsTable = new Hashtable();
sessionsTable.put(id, System.currentTimeMillis()
}
public void sessionDestroyed(HttpSessi
String id = event.getSession().getId()
if(sessionsTable.containsK
long aliveTime = System.currentTimeMillis()
aliveTimeList.add(new Long(aliveTime));
sessionsTable.remove(id);
}
}
}
// TO DO: TimerTask that will get Count and list and write to log file.
// think about synchronization more
// I will have more time to work on this weekend.
Can I avoid writing a custom class completely by using twiddle? I know next to nothing about twiddle, and the little I do know I have learned in the past hour by Googling it. From what I can see it will allow me to execute a command which will return any one of the values which is available via the JBoss Management Console.
Is this easier then writing a class?
Hi Tomas,
One person suggested I have a look at the "JBoss Management Console" (http://my-server:8180/web
"...then in the console function tree find: Monitoring - > Web Status -> Full Status.
You will get a page with loads of stats on the right side.
Find [Application list] and link to your application, simply click on it
and at the top of the page you will see all the stats that you need..."
I had a look at this and it looks perfect - it gives me more information than I was expecting, like Active sessions, Session count, Max active sessions, Rejected session creations, Expired sessions, Longest session alive time, and Average session alive time. I am really only interested in Active Sessions.
Do you know of a way to retrieve that information? I assume that because the number of Active Sessions is available in the JBoss Management Console, that there is some kind of API which can be called to return that information.
Is there any difference between the information provided in the JBoss Management Console and the session information that would be provided by a custom class (eg: one of the samples you provided links for)?
Cheers,
Paul Hobbs
Hi Paul!
Well the JBoss Management Console is an MBean (Management Bean) so if you know how to connect to a MBean then you could retrieve
this information :)
http://wiki.jboss.org/wiki
http://www.informit.com/gu
http://lists.jboss.org/pip
http://java.sun.com/j2se/1
http://java.sun.com/develo
Hope this helps.
Regards,
Tomas Helgi
Hi Tomas,
If I knew how to connect to an MBean it might help. Alas, I know nothing about MBeans or how to connect to them. Also, regrettably I don't have time to spend hours learning about MBeans - my TO DO list is long enough as it is!
I am looking for a quick fix here - someone who has done something similar before (or even the same) and who is able to share their code or specific steps. If you look at the image I attached in an earlier post, it gives you a clear idea of what I want to achieve. The bits I need help with are steps 2 and 3.
Cheers,
Paul
Here are some MBean examples
http://www.java2s.com/Code
http://www.java2s.com/Code
http://www.java2s.com/Code
And something on the JBoss Managent Console as well
http://wiki.jboss.org/wiki
http://wiki.jboss.org/wiki
http://wiki.jboss.org/wiki
And here is some MBean statistics example
http://www.hibernate.org/2
Regards,
Tomas Helgi
>I am really only interested in Active Sessions.
>I am looking for a quick fix here
Ok, just use a listener.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionListener implements HttpSessionListener {
HashSet sessionSet = null;
ServletContext application = null;
public void sessionCreated(HttpSession
HttpSession session = event.getSession();
if(application == null)application = session.getServletContext(
sessionSet = (HashSet)application.getAt
if(sessionSet == null){
application.setAttribute("
sessionSet = (HashSet)application.getAt
}
sessionSet.add(session.get
}
public void sessionDestroyed(HttpSessi
sessionSet.remove(event.ge
}
}
Register it in your web app's web.xml with
<listener>
<listener-class>rrz.Sessio
</listener>
and test it on a JSP with
<%
session.setMaxInactiveInte
%>
Session count is <%=((HashSet)application.g
Hi rrz,
Instead of using a JSP to call that class, can the class be called from a batch file? Or even better (from my perspective) - can that class simply output the session count to a file? I am guessing that the SessionListener class is automatically called every time a session is either created or destroyed, which is what increments/decrements the variable sessionSet. The JSP is simply retrieving the current value stored in the variable and displaying it in a browser. Is my guess correct? I am not interested in displaying the session count in a browser - I want that session count in a log file.
I have found a code sample which shows me how to output a string to a file (http://www.java2s.com/Cod
Cheers,
Paul
Hi rrz and Tomas,
I have finally solved the problem. Someone on another forum pointed me in the direction of the specific MBean I needed, so I wrote a little VBScript batch file which gets the activeSessions attribute from the MBean. I have attached the code for the VBScript file.
Thanks for trying to help out. I am happy to split the points evenly between you (if that is OK with you).
Cheers,
Paul
Business Accounts
Answer for Membership
by: TomasHelgiPosted on 2008-08-20 at 07:29:41ID: 22269904
Hi!
/Java/Serv lets/ Servl etsessionl istener.ht m /Java/Serv lets/Sessi onlogger.h tm /Java/JSP/ JSPsession counter.ht m /5/docs/ap i/javax/se rvlet/http / HttpSessi onListener .html
What I suggest you do is create an HttpSessionListener (see first example below)
and use that to monitor and track the sessions.
http://www.java2s.com/Code
http://www.java2s.com/Code
http://www.java2s.com/Code
http://java.sun.com/javaee
Regards,
Tomas Helgi