2011-08-29 60 views
2

一個應用程序使用JBoss 4.2.2,我發現它有必要調用listThreadDump(),我期望它在ServerInfo調用JBoss MBean函數來獲取threaddump

我在想我需要找到這個信息的jar是jboss-jmx.jar。

那麼,如何通過調用類似於http://localhost:8080/jmx-console/HtmlAdaptor?action=invokeOpByName&name=jboss.system:type=ServerInfo&methodName=listThreadDump的東西以編程方式複製所做的事情?

回答

3

這就是我如何訪問ServerInfo MBean。我使用的是JBoss AS 5.1,但這種方法應該是一樣的。

要使用MBeanServer實例調用listThreadDump(),您可以使用invoke()上的ServerInfo MBean方法。

此外,您可以使用相同的MBeanServer訪問MBean的屬性。

示例代碼:

// imports 
import javax.management.MBeanServer; 
import javax.management.ObjectName; 
import org.jboss.mx.util.MBeanServerLocator; 

try { 
    MBeanServer server = MBeanServerLocator.locate(); 
    ObjectName name = new ObjectName("jboss.system:type=ServerInfo"); 
    // invoke the listThreadDump method and capture its output 
    String threadDumpHtml = (String) server.invoke(name, "listThreadDump", null, null); 

    // access a simple attribute of the ServerInfo object 
    String jvmName = (String) server.getAttribute(name, "JavaVMName"); 
} catch (Exception e) { 
    // Ideally catch the 3 exact exceptions 
} 

最後,我覺得很方便,當MBean公開的「實例」屬性,這樣,那麼你可以直接訪問對象(CastToType) server.getAttribute(name, "instance"),而不是總是通過的MBeanServer去。例如,當使用JMS時,ServerPeer實例很有用,因爲您可以在隊列和主題訂戶上獲得消息計數器。