调用远程运行在tomcat上的MBean操作

时间:2014-10-24 06:15:18

标签: tomcat rmi jmx

我写了一个MBean来获取所有正在运行的线程的某些属性。在Tomcat上运行它。

..
public String dumpThreads() {

        ThreadMXBean threadMX = ManagementFactory.getThreadMXBean();
        long[] allThreadIDs = threadMX.getAllThreadIds();
        ThreadInfo[] threadsInfo = threadMX.getThreadInfo(allThreadIDs, 10);
        StringBuilder threadDumpSB = new StringBuilder();

        threadDumpSB.append ("THREAD DUMP - " + (new SimpleDateFormat ("dd-MMM-yyyy HH:mm:ss")).format(new Date()) );
        threadDumpSB.append ("\n\n");
        threadDumpSB.append ("Thread count: " + Integer.toString(threadsInfo.length) );
        threadDumpSB.append ("\n\n\n");

        for (ThreadInfo threadInfo : threadsInfo) {
            threadDumpSB.append ("ID: " + Long.toString(threadInfo.getThreadId()) + "\n");
            threadDumpSB.append ("Name: " + threadInfo.getThreadName() + "\n");
            threadDumpSB.append ("State: " + threadInfo.getThreadState() + "\n");
            StackTraceElement[] stacktraceElements = threadInfo.getStackTrace();
            for (StackTraceElement stElm : stacktraceElements) 
                threadDumpSB.append ("    at " + stElm.toString() + "\n");
            threadDumpSB.append ("\n");
        }

        return (threadDumpSB.toString());

    }
..

我可以通过jconsole在应用程序>服务器>服务器>操作以及http:// servername :8080 / manager / jmxproxy

Name: Application:Name=Server,Type=Server
modelerType: org.puneet.mbeans.Server
Uptime: 3156547

现在,我想远程执行 dumpThreads() 方法。

到目前为止,这是我要去的地方:

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8008/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

我可以连接到服务器&得到一个mbeans等的列表。不知道如何获得我的mbean&然后调用dumpThreads()操作。

ObjectName mbeanName = new ObjectName("Application:Name=Server,Type=Server"); ...?

提前致谢。

普尼特

1 个答案:

答案 0 :(得分:0)

想出来了!

...
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
...
ObjectName mbeanName = new ObjectName("Application:Name=Server,Type=Server");
Object o = mbsc.invoke(mbeanName, "dumpThreads", null, null);
System.out.println(o);
...