Reputation: 58826
How can I use JMX to access the threads of a remote JVM?
Upvotes: 0
Views: 3243
Reputation: 279
This is a very old thread, but since I was looking and found the actual answer to this question I may as well post it.
The above shows a very nice example on how to get access to all threads on a remote JMX.
In Synopsis:
// First connect to the remote service
String urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi";
JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath);
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection server = jmxc.getMBeanServerConnection();
// Then get all threads
ThreadMXBean tmbean = newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, ThreadMXBean.class);
ObjectName tmbean = new ObjectName(THREAD_MXBEAN_NAME);
long[] tids = tmbean.getAllThreadIds();
ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
for (ThreadInfo ti : tinfos) {
// do something with this particular thread...
}
To emphasize the possible need to want to write your own monitoring tool as questioned by @aaron-mciver; threads contain a lot of information and in a business process there can be a lot of threads and it may not be possible to look through them by eye-balling them in VisualVM for instance. It can make a lot of sense to programmatically loop through the threads to look for specific keywords, CPU times, stack traces, filenames involved etc...
Upvotes: 4
Reputation: 24723
Why not use jvisualvm as it comes as part of the SDK and will give you access to the threads within a given JVM amongst other things.
No point in attempting to replicate debugging features which already exist.
Upvotes: 1