How to get the JMXConnectorServer of the platform MBeanServer?

走远了吗. 提交于 2020-01-03 05:28:09

问题


I have a Java program providing services which can be invoked by calling methods on an JMX MBean via RMI. The service is running without problems, but I am facing the question of how to shut down the service without interrupting a potential new concurrent request to the service.

One solution for this problem would be to wait for all JMX connections to be closed and only then (and when there is no more background activity) to shut down the process. JMXConnectorServer has a method getConnectionIds() that I could use for that, but I already got stuck with the following question:

How do I get the JMXConnectorServer instance of the platform MBean server, i.e. the server returned by ManagementFactory.getPlatformMBeanServer()?


回答1:


AFAIK, it is not possible to get the JMXConnectorServer which is automatically created by getPlatformMBeanServer(), but you can make the platform MBean server use a connector server instance that you created yourself.

When you do this, it is important that the com.sun.management.jmxremote* system properties are unset, so that the platform MBean server does not automatically set up a connector server.

Example: If you configured the JMX remote access with the system properties

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

remove these system properties and configure your own connector server programmatically with the following code:

int jmxPort = 1919;
LocateRegistry.createRegistry(jmxPort);

MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
// or: MBeanServer beanServer = MBeanServerFactory.createMBeanServer(); // doesn't register the default platform MBeans

JMXServiceURL jmxUrl 
    = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + jmxPort + "/jmxrmi");
JMXConnectorServer connectorServer
    = JMXConnectorServerFactory.newJMXConnectorServer(jmxUrl, null, beanServer);

connectorServer.start();

This tech note from Oracle contains another example for a manual connector server setup when you want to use authentication and SSL.



来源:https://stackoverflow.com/questions/23273579/how-to-get-the-jmxconnectorserver-of-the-platform-mbeanserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!