Hi,

This article is very important in case of JBoss running as a Windows Service, Because if we run any Server JBoss or any other application server as a Windows Service then NONE of the JVM utilities like “jmap”, “jconsole” , “jps” or “VisualVM”… etc will work. So it becomes very difficult to collect the Thread Dump or Heap Dump of the Running Service.

We wrote an article based on JMX to collect the JVM’s memory related statistics using JMX code “Monitoring Heap & NonHeap Memory of JBoss using MBean code. Here we are extending the same program to make the JMX program to even collect the HeapDump whenever we want.

To connect to the actual JConsole remotely also we need to provide the following JAVA_OPTS inside our “$JBOSS_HOME/bin/run.bat” (run.conf.bat) file the connection url format should be:

service:jmx:rmi:///jndi/rmi://"+hostname+":"+port+"/jmxrmi

Step1). Edit the “%JBOSS_HOME%/bin/run.conf.bat” or “%JBOSS_HOME%/bin/run.bat” (Server StartScript configuration file) and then set the following JAVA_OPTS there.

set JAVA_OPTS="%JAVA_OPTS% -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=4545 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver"

Make sure that the 4545 is not being used by any other process. Or define any other port in above parameter which is free.

Step2). Now from any other box or from the same Box you can run the Following JMX code to generate the HeapDump of the JVM process:
“GenerateHeapDump.java”

import java.util.Hashtable;
import java.io.*;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.management.*;
import javax.management.openmbean.CompositeDataSupport;

public class GenerateHeapDump
{
	private static MBeanServerConnection connection;
	private static JMXConnector connector;
	public static void Connection(String hostname, String port) throws IOException
	{
		Integer portInteger = Integer.valueOf(port);
		Hashtable h = new Hashtable();
		JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+hostname+":"+port+"/jmxrmi");
		connector = JMXConnectorFactory.connect(address,null);
		connection = connector.getMBeanServerConnection();
		System.out.println("GOT THE MBeanServerConnection---SUCCESSFULLY");
	}

	private static void doGarbageCollection() throws Exception
	{
		ObjectName memoryMXBean=new ObjectName("java.lang:type=Memory");
		connection.invoke(memoryMXBean,"gc", null, null);
		System.out.println("nnt------Garbage Collection Done Successfully-----");
	}

	private static void getHeapDump(String fileName) throws Exception
	{
		ObjectName memoryMXBean=new ObjectName("com.sun.management:type=HotSpotDiagnostic");
		Object[] params = new Object[] { "C:/WindowsService_Script/"+fileName, Boolean.TRUE };
		String[] signature = new String[] { String.class.getName(), boolean.class.getName() };
		Object result = connection.invoke(memoryMXBean, "dumpHeap" , params, signature);
		System.out.println("nt Heap Dump Generated to " +fileName);
	}

	public static void main(String[] args) throws Exception
	{
	String hostname = args[0];
	String port = args[1];
	Connection(hostname, port);
	System.out.println("nt----------Generating Heap Dump---------");
	getHeapDump("HeapDump.hprof");
	connector.close();
	}
}

Step3). Run your JBoss Windows Service and then compile and run your Program to generate the HeapDump

C:WindowsService_Script>set PATH=c:jdk160_05bin;%PATH%;
C:WindowsService_Script>javac GenerateHeapDump.java

C:WindowsService_Script>java GenerateHeapDump 10.10.10.10 4545
GOT THE MBeanServerConnection---SUCCESSFULLY

        ----------Generating Heap Dump---------
         Heap Dump Generated to HeapDump.hpro

NOTE: you will find the generated HeapDump in the "C:/WindowsService_Script/" directory in the Box where the JBoss is running as a Service which is defined in line -39 in above code.

.
.
Thanks
Middleware Magic Team

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.