Question

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()?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top