Programming Example

The following example shows how you might connect to the JMX agent using the RMIConnector and manipulate the AdminDistributedSystem MBean.

JMXServiceURL url = new
    JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxconnector");
JMXConnector conn = JMXConnectorFactory.connect(url);
MBeanServerConnection mbsc = conn.getMBeanServerConnection();
ObjectName agentName = new ObjectName("GemFire:type=Agent");
ObjectName distName = (ObjectName) mbsc.invoke(agentName, "connectToSystem", 
    new Object[] {}, new String[] {});

MBeanInfo distInfo = mbsc.getMBeanInfo(distName);

String description = distInfo.getDescription();
MBeanAttributeInfo[] attrs = distInfo.getAttributes();
MBeanConstructorInfo[] ctors = distInfo.getConstructors();
MBeanNotificationInfo[] nots = distInfo.getNotifications();
MBeanOperationInfo[] opers = distInfo.getOperations();

The first line of the example specifies the machine (localhost) and port (1099) of the machine on which the JMX Agent is running.

JMXServiceURL url = new 
    JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxconnector");
JMXConnector conn = JMXConnectorFactory.connect(url);

The next line creates a connection to the MBeanServer.

MBeanServerConnection mbsc = conn.getMBeanServerConnection();

The next line specifies the ObjectName of the JMX Agent. All MBeans are referenced using an ObjectName. If you don’t know the ObjectName, you can use the querying capabilities of the MBeanServer to obtain it.

ObjectName agentName = new ObjectName("GemFire:type=Agent");

The JMX Agent connects to the AdminDistributedSystem and registers an AdminDistributedSystem MBean and returns the ObjectName for this AdminDistributedSystem MBean instance.

ObjectName distName = (ObjectName) mbsc.invoke(agentName, "connectToSystem", new Object[] {}, 
    new String[] {});

The next line asks the MBeanServer for information about the specified AdminDistributedSystem. JMX allows you to obtain all of this information programmatically.

MBeanInfo distInfo = mbsc.getMBeanInfo(distName);

These lines provide detailed information: a description of the distributed system, its attributes, constructors, notifications, and operations.

String description = distInfo.getDescription();
MBeanAttributeInfo[ ] attrs = distInfo.getAttributes();
MBeanConstructorInfo[ ] ctors = distInfo.getConstructors();
MBeanNotificationInfo[ ] nots = distInfo.getNotifications();
MBeanOperationInfo[ ] opers = distInfo.getOperations();