Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow restarting Java application with JMX monitoring enabled immediately

I have a Java application with JMX monitoring enabled like this:

-Dcom.sun.management.jmxremote.port=9999 \
// some other properties omitted

But when I try to restart the application, sometime I got an error says the JMX port number is already in use. This is not acceptable.

So I want to set the SO_REUSEADDR to true for the underlying socket to avoid this error but found no related JMX properties.

Any idea?

like image 540
George Avatar asked May 08 '14 02:05

George


People also ask

How do I enable JMX in Java application?

A common way to enable local JMX access on these JVMs is to include the -Dcom. sun. management. jmxremote option on the command line when you start the JVM.

What is JMX monitoring?

Java Management Extensions (JMX) is a specification for monitoring and managing Java applications. It enables a generic management system to monitor your application; raise notifications when the application needs attention; and change the state of your application to remedy problems.

What is JMX monitoring Minecraft?

JMX Monitoring This enables server admins to hook alerts and graphing tools using ordinary JMX clients and dashboards.


2 Answers

I had the same issue. It was the first instance of my application (which I had stopped) which was still subscribed to this port, so the new instance could not start. In my case it didn't have to do with the socket TIME_WAIT mechanism but rather with the fact that after calling stop(), it took some time until all running threads ended gracefully. What worked in my case was unregistering the bean right before stopping the application so that the socket is free.

private void unregisterBeanForName(String name) {
        try {

            JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:9999/jmxrmi");
            JMXConnector cc = JMXConnectorFactory.connect(jmxServiceURL);
            MBeanServerConnection mbsc = cc.getMBeanServerConnection();
//This information is available in jconsole
            ObjectName serviceConfigName = new ObjectName("YourObjectName");
            mbsc.unregisterMBean(serviceConfigName);
// Close JMX connector
            cc.close();
        } catch (Exception e) {
            System.out.println("Exception occurred: " + e.toString());
            e.printStackTrace();
        }
    }
like image 143
Katerina A. Avatar answered Sep 20 '22 06:09

Katerina A.


I am afraid you can't do that from command line.

You will need to create a RMIServerSocketFactory, which produces ServerSockets with the desired option (SO_REUSEADDR).

Docs here: http://docs.oracle.com/javase/8/docs/technotes/guides/rmi/socketfactory/

Someone else solving the same problem: https://svn.apache.org/viewvc?view=revision&revision=r1596579

like image 42
Fox Avatar answered Sep 20 '22 06:09

Fox