java 注册到 mbean 服务器的 Mbean 未显示在 jconsole 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7424009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:55:15  来源:igfitidea点击:

Mbeans registered to mbean server not showing up in jconsole

javajmxmbeans

提问by Prasanna

I create a mbean server using MBeanServerFactory.createMBeanServer and register mbeans with it. I can find the mbean server in jconsole but when I connect to it I do not see registered mbeans. Here is the code:

我使用 MBeanServerFactory.createMBeanServer 创建一个 mbean 服务器并用它注册 mbeans。我可以在 jconsole 中找到 mbean 服务器,但是当我连接到它时,我看不到已注册的 mbean。这是代码:

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)
    {
    }
}

Instead of creating a mbean server, if I using the platformMBeanServer and register my mbean to it, I can see the mbean in jconsole. Any idea what else do I need to do while doing createMBeanServer?

如果我使用 platformMBeanServer 并将我的 mbean 注册到它,而不是创建 mbean 服务器,我可以在 jconsole 中看到 mbean。知道在执行 createMBeanServer 时我还需要做什么吗?

回答by thusitha

I came accross this issue yesterday but managed to sort that one out. I spend some time doing that so I thought this post would be helpfull to save someone else's time.

我昨天遇到了这个问题,但设法解决了这个问题。我花了一些时间这样做,所以我认为这篇文章将有助于节省别人的时间。

First you can register you beans in the java code as stated in your post in the main method. But I think it is much more simpler if you use Spring.

首先,您可以按照您在 main 方法中的帖子所述,在 Java 代码中注册您的 bean。但我认为如果使用 Spring 会简单得多。

Check this link out for more information; http://static.springsource.org/spring/docs/2.0.x/reference/jmx.html

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

There are some loop holes you need to avoid. Don't start two MBeans servers in your application.

您需要避免一些漏洞。不要在您的应用程序中启动两个 MBeans 服务器。

I used this config file:

我使用了这个配置文件:

<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>

Use this configuration to attach a bean name to MBeanExporter. Make sure 'lazy-init' is set to false. Please note I am using this configuration in a webapplication. Web application is deployed inside tomcat. So tomcat already has MBean server. So you don't need to provide one explicitly. If you are running it in a standalone mode you need to start a MBean server and configure it accordingly.

使用此配置将 bean 名称附加到 MBeanExporter。确保 'lazy-init' 设置为 false。请注意,我在 Web 应用程序中使用此配置。Web 应用程序部署在 tomcat 内部。所以tomcat已经有了MBean服务器。所以你不需要明确提供一个。如果您在独立模式下运行它,您需要启动 MBean 服务器并相应地配置它。

Please also note you need add following properties inside tomcat's catalina.bat file. set 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"

另请注意,您需要在 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 connector port in this case is 8088 and hostname is 'localhost' Once you have started tomcat you need to start jconsole(I am not going to tell how to start it here) Then click on 'RemoteProcess' and type 'localhost:8088' and connect to the tomcat's MBean server. Then go to MBean tab in jconsole and you should see your MBean there.

在这种情况下,Jmx 连接器端口是 8088,主机名是 'localhost' 一旦你启动了 tomcat,你需要启动 jconsole(我不会在这里告诉如何启动它)然后点击 'RemoteProcess' 并输入 'localhost:8088'并连接到 tomcat 的 MBean 服务器。然后转到 jconsole 中的 MBean 选项卡,您应该在那里看到您的 MBean。

回答by r00k

If you want to see your MBeans in the JConsole, you will have to use RMI. Basically, do the following

如果要在 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

回答by oberlies

The easiest solution is to use the Platform MBean Server configured via system properties.

最简单的解决方案是使用通过系统属性配置的 Platform MBean Server 。

So you need to the MBeanServerinstance with

所以你需要MBeanServer实例

MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();

and set the following system properties when launching your application:

并在启动应用程序时设置以下系统属性:

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

回答by user249654

You need use PlatformMBeanServer

您需要使用 PlatformMBeanServer

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

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

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