Tomcat fails to stop with "Port already in use" when enabling jmx remote monitoring

Solution Verified - Updated

Environment

  • JBoss Enterprise Web Server (EWS)
    • Tomcat 5
    • Tomcat 6
    • Tomcat 7

Issue

We are setting the following java options to JAVA_OPTS in order to enable jmx remote monitoring:

JAVA_OPTS="$JAVA_OPTS -Xms1024m -Xmx2048m"
JAVA_OPTS="$JAVA_OPTS -XX:PermSize=256m -XX:MaxPermSize=256m"
JAVA_OPTS="$JAVA_OPTS -verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps"
JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote=true"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=12345" 

Then the following error is shown when executing catalina.sh stop or service tomcat6 stop command:

Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 12345; nested exception is: 
	java.net.BindException: Address already in use

Then we have to stop Tomcat java process with kill command like: $kill <TOMCAT_JAVA_PID>

Resolution

Please set java options to CATALINA_OPTS instead of JAVA_OPTS like:

CATALINA_OPTS="$CATALINA_OPTS -Xms1024m -Xmx2048m"
CATALINA_OPTS="$CATALINA_OPTS -XX:PermSize=256m -XX:MaxPermSize=256m"
CATALINA_OPTS="$CATALINA_OPTS -verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps"
CATALINA_OPTS="$CATALINA_OPTS -XX:+HeapDumpOnOutOfMemoryError"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote=true"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=12345" 

in $CATALINA_HOME/bin/setenv.sh

Root Cause

CATALINA_OPTS is only used as java options for Tomcat java process. However JAVA_OPTS is not only used for Tomcat java process but also used for another java process which catalina.sh stop starts up for stopping Tomcat. If you set the above options to JAVA_OPTS, the above java option is set to the another java process. So it causes the another java process tries to open the same jmxremote.port 12345, which is already used by Tomcat java process. Then it results in such a "Port already in use" error.

Components
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.