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?
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.
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.
JMX Monitoring This enables server admins to hook alerts and graphing tools using ordinary JMX clients and dashboards.
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();
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With