Remote JMX client
Add these properties to the target VM to specify the jmx port and allow remote connections so you can get to it from other systems without the need for authentication.
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=9999"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
The example below demonstrates the following:
- Connection
- Listing all MBeans
- Listing all MBean attributes, types, and mutability
- Listing all MBean operations, parameters, and return types
- Getting an attributes current value
- Setting an attributes value
- Invoking operations
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class MBeanClient
{
public static void main(String[] args) throws IOException, MalformedObjectNameException, NullPointerException,
InstanceNotFoundException, ReflectionException, IntrospectionException, AttributeNotFoundException,
MBeanException, InvalidAttributeValueException
{
// Connect to server
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
// List all MBeans
Set names = mbsc.queryNames(null, null);
for (Iterator i = names.iterator(); i.hasNext();)
{
ObjectName on = (ObjectName) i.next();
System.out.println("\tObjectName = " + on);
listAttributes(mbsc, on.getCanonicalName());
listOperations(mbsc, on.getCanonicalName());
}
// get value of SomeAttributeName
ObjectName someMBeanName = new ObjectName("TopNodeName:subNodeName=Foo,mbeanName=FooBar");
System.out.println("MaxFileSize: " + mbsc.getAttribute(someMBeanName, "SomeAttributeName"));
// set value of SomeAttributeName
mbsc.setAttribute(someMBeanName, new Attribute("SomeAttributeName", 10));
// get new value of SomeAttributeName
System.out.println("SomeAttributeName: " + mbsc.getAttribute(someMBeanName, "SomeAttributeName"));
// invoke operation
mbsc.invoke(someMBeanName, "someOperationName", null, null);
}
private static void listAttributes(MBeanServerConnection mbsc, String canonicalName)
throws MalformedObjectNameException, NullPointerException, InstanceNotFoundException, IntrospectionException,
ReflectionException, IOException
{
// List all mbean attributes
ObjectName objectName = new ObjectName(canonicalName);
MBeanInfo mbeanInfo = mbsc.getMBeanInfo(objectName);
MBeanAttributeInfo[] attrInfo = mbeanInfo.getAttributes();
for (MBeanAttributeInfo info: attrInfo)
{
System.out.println(info);
}
}
private static void listOperations(MBeanServerConnection mbsc, String canonicalName) throws MalformedObjectNameException, NullPointerException, InstanceNotFoundException, IntrospectionException, ReflectionException, IOException
{
ObjectName objectName = new ObjectName(canonicalName);
MBeanInfo mbeanInfo = mbsc.getMBeanInfo(objectName);
MBeanOperationInfo[] operInfo = mbeanInfo.getOperations();
for (MBeanOperationInfo info: operInfo)
{
System.out.println(info);
}
}
}
Comments