-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumpTomcatSessions.java
62 lines (51 loc) · 1.94 KB
/
DumpTomcatSessions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// DumpTomcatSessions.java
import java.util.HashMap;
import javax.management.remote.*;
import javax.management.*;
public class DumpTomcatSessions {
public static void main(String[] args) {
if(args.length < 2 || args.length > 4) {
System.err.println("\nUsage: java DumpTomcatSessions server port [user password]\n");
System.exit(1);
}
String server = args[0];
String port = args[1];
if(args.length == 4) {
String user = args[2];
String password = args[3];
dumpSessions(server,port,user,password);
}
else {
dumpSessions(server,port,null,null);
}
}
static void dumpSessions(String server, String port, String user, String password) {
try {
HashMap<String, String[]> environment = null;
if(user != null && password != null)
{
environment = new HashMap<>();
String[] credentials = new String[] { user, password };
environment.put(JMXConnector.CREDENTIALS, credentials);
}
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + server + ":" + port + "/jmxrmi");
JMXConnector conn = JMXConnectorFactory.connect(url,environment);
MBeanServerConnection mbsc = conn.getMBeanServerConnection();
ObjectName catman = new ObjectName("Catalina:type=Manager,context=*,host=*");
for(ObjectInstance x : mbsc.queryMBeans(catman, null)) {
Integer num_sessions = (Integer)mbsc.getAttribute(x.getObjectName(), "activeSessions");
if(num_sessions > 0) {
System.out.println(x.getObjectName().toString() + " total sessions = " + num_sessions);
Object result = mbsc.invoke(x.getObjectName(), "listSessionIds", null, null);
String[] sessions = result.toString().split(" ");
for(int i=0;i < sessions.length;i++) {
System.out.println(sessions[i]);
}
}
}
System.exit(0);
} catch(Exception e) {
e.printStackTrace();
}
}
}