簡體   English   中英

注冊到mbean服務器的Mbeans沒有出現在jconsole中

[英]Mbeans registered to mbean server not showing up in jconsole

我使用MBeanServerFactory.createMBeanServer創建一個mbean服務器並使用它注冊mbeans。 我可以在jconsole中找到mbean服務器,但是當我連接到它時,我看不到已注冊的mbeans。 這是代碼:

public static void main(String[] args) throws Exception
{
    MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer("example");
    ObjectName objectName = new ObjectName("example:type=simpleMbean");
    Simple simple = new Simple (1, 0);
    mbeanServer.registerMBean(simple, objectName);
    while (true)
    {
    }
}

如果我使用platformMBeanServer並將mbean注冊到它,而不是創建一個mbean服務器,我可以在jconsole中看到mbean。 知道做createMBeanServer時我還需要做什么嗎?

我昨天遇到了這個問題,但設法將那個問題排除在外。 我花了一些時間這樣做,所以我認為這篇文章有助於節省別人的時間。

首先,您可以在主要方法中的帖子中說明的java代碼中注冊bean。 但我認為如果你使用Spring會更簡單。

查看此鏈接以獲取更多信息; http://static.springsource.org/spring/docs/2.0.x/reference/jmx.html

您需要避免一些環孔。 不要在應用程序中啟動兩個MBean服務器。

我用過這個配置文件:

<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=beanName" value-ref="dataSource"/>
</map>
</property>
  <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
</bean>

使用此配置將Bean名稱附加到MBeanExporter。 確保'lazy-init'設置為false。 請注意我在webapplication中使用此配置。 Web應用程序部署在tomcat中。 所以tomcat已經有了MBean服務器。 所以你不需要明確提供一個。 如果以獨立模式運行它,則需要啟動MBean服務器並相應地對其進行配置。

另請注意,您需要在tomcat的catalina.bat文件中添加以下屬性。 設置CATALINA_OPTS = -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port = 8088 -Dcom.sun.management.jmxremote.ssl = false -Dcom.sun.management.jmxremote.authenticate = false -Dcom。 sun.management.jmxremote.hostname = “localhost” 的

在這種情況下Jmx連接器端口是8088,主機名是'localhost'一旦你啟動了tomcat,你需要啟動jconsole(我不打算告訴如何在這里啟動它)然后點擊'RemoteProcess'並輸入'localhost:8088'並連接到tomcat的MBean服務器。 然后轉到jconsole中的MBean選項卡,你應該在那里看到你的MBean。

最簡單的解決方案是使用通過系統屬性配置的Platform MBean Server。

所以你需要使用MBeanServer實例

MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();

並在啟動應用程序時設置以下系統屬性:

-Dcom.sun.management.jmxremote.port=1919
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

如果要在JConsole中查看MBean,則必須使用RMI。 基本上,請執行以下操作

Registry registry = LocateRegistry.createRegistry(RMI_REGISTRY_PORT);
//... create your MBean Server here and add your MBeans...
Map<String, Object> env = new HashMap<String, Object>(); //Add authenticators and stuff to the environment.    

//Create a URL from which your beans will be accessible.    
JMXServiceURL jmxServiceURL = new JMXServiceURL("rmi", 
                                                "localhost", 
                                                CONNECTOR_SERVER_PORT, 
                                                "/jndi/rmi://localhost:" + RMI_REGISTRY_PORT + "myApp");

//Start the connector server
JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, env, server);
System.out.println(jmxServiceURL); //Use this URL to connect through JConsole, instead of selecting Local Process, just select the Remote process

您需要使用PlatformMBeanServer

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

ps http://www.javalobby.org/java/forums/t49130.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM