I am running a client/server application using JBoss.
How can I connect to the server JVM's MBeanServer? I want to use the MemoryMX MBean to track the memory consumption.
I can connect to the JBoss MBeanServer using JNDI lookup but the java.lang.MemoryMX MBean is not registered with the JBoss MBeanServer.
EDIT: The requirement is for programmatic access to the memory usage from the client.
I wrote a class like this:
import javax.management.remote.JMXServiceURL;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
public class JVMRuntimeClient
{
static void main(String[] args) throws Exception
{
if (args == null)
{
System.out.println("Usage: java JVMRuntimeClient HOST PORT");
}
if(args.length < 2)
{
System.out.println("Usage: java JVMRuntimeClient HOST PORT");
}
try
{
JMXServiceURL target = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+args[0]+":"+args[1]+"/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(target);
MBeanServerConnection remote = connector.getMBeanServerConnection();
/**
* this is the part where you MUST know which MBean to get
* com.digitalscripter.search.statistics:name=requestStatistics,type=RequestStatistics
* YOURS WILL VARY!
*/
ObjectName bean = new ObjectName("com.digitalscripter.search.statistics:name=requestStatistics,type=RequestStatistics");
MBeanInfo info = remote.getMBeanInfo(bean);
MBeanAttributeInfo[] attributes = info.getAttributes();
for (MBeanAttributeInfo attr : attributes)
{
System.out.println(attr.getDescription() + " " + remote.getAttribute(bean,attr.getName()));
}
connector.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
}
}