Jay SenSharma

Jay SenSharma

In continuation to the post: HttpSession Monitoring Using <monitoring-attribute-name>)

So please configure the Env and Application like mentioned in above link first.

Here is a JMX Code to Monitor the Total Number of Users and their UserNames, who has currently logged to Our Application in which we have already enabled the HttpSession Monitoring and the Monitoring Addribute.

This JMX code is very important when u dont want to Access the AdminConsole or Remotely u want to know how many users are currently accessing your Applications with Active HttpSessions.

Step1).Write the Following JMX Code “HttpSessionMonitoring.java” Somewhere in your File System…

Example: C:SessionMonitoringWebApp
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

public class HttpSessionMonitoring
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;

static {
try {
service = new ObjectName(
}catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());

public static void initConnection(String hostname, String portString, String username, String password) throws IOException, MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();

public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes");

public void printNameAndState() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
System.out.println(" Server   State");
int length = (int) serverRT.length;
for (int i = 0; i < length; i++) {
String name = (String) connection.getAttribute(serverRT[i],"Name");
String state = (String) connection.getAttribute(serverRT[i],"State");
System.out.println( name + " : " + state);

public void getServletData() throws Exception
ObjectName[] serverRT = getServerRuntimes();
int length = (int) serverRT.length;

for (int i = 0; i < length; i++)
ObjectName[] appRT = (ObjectName[]) connection.getAttribute(serverRT[i], "ApplicationRuntimes");
int appLength = (int) appRT.length;

for (int x = 0; x < appLength; x++)
String applicationName=(String)connection.getAttribute(appRT[x], "Name");
if(applicationName.equals("consoleapp") || applicationName.equals("bea_wls_internal") || applicationName.equals("mejb") )
ObjectName[] compRT =  (ObjectName[]) connection.getAttribute(appRT[x], "ComponentRuntimes");
int compLength = (int) compRT.length;

for (int y = 0; y < compLength; y++)
String componentType = (String) connection.getAttribute(compRT[y], "Type");
if (componentType.toString().equals("WebAppComponentRuntime"))
System.out.println("nApplication name: " +applicationName);
ObjectName[] servletRTs = (ObjectName[])
connection.getAttribute(compRT[y], "Servlets");
int servletLength = (int) servletRTs.length;

for (int z = 0; z < servletLength; z++)
System.out.println("       Servlet name: " + (String)connection.getAttribute(servletRTs[z],"Name"));
System.out.println("       Servlet context path: " + (String)connection.getAttribute(servletRTs[z], "ContextPath"));
System.out.println("       Invocation Total Count : " + (Object)connection.getAttribute(servletRTs[z],  "InvocationTotalCount"));
String servletSessionsMonitoringIds[] = (String[]) connection.getAttribute(compRT[y], "ServletSessionsMonitoringIds");
System.out.println("nntTotal Number of Users Logged in to Application = "+servletSessionsMonitoringIds.length);;
for(int ids=0;ids<servletSessionsMonitoringIds.length;ids++)
System.out.println("ntCurrently Logged In Users List Below:n ");
System.out.println("ttUserCount: "+(ids+1)+"----------Name: "+servletSessionsMonitoringIds[ids]);
public static void main(String[] args) throws Exception {
if(args.length < 4) {
System.out.println("<Usage>: java HttpSessionMonitoring  adm-host  adm-port adm-username adm-password");
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
HttpSessionMonitoring s = new HttpSessionMonitoring();
initConnection(hostname, portString, username, password);
Step2).Open a Command Window and then run “. ./setWLSEnv.sh” or “setWLSEnv.cmd” then run the command to compile and Run the above program like following:
C:SessionMonitoringWebApp>javac -d . HttpSessionMonitoring.java
C:SessionMonitoringWebApp>java HttpSessionMonitoring localhost 7001 weblogic weblogic
Monitoring HttpSessions using JMX Code

Monitoring HttpSessions using JMX Code

Jay SenSharma
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.