The purpose of this examples is to demostrate the implementation of a standard MBean and a dynamic MBean. It also shows how to perform operations on them, both locally, and remotely through an RMI connection between a server and a remote client.
A standard MBean is one that statically defines its management interface through the name of the methods it contains. A dynamic MBean implements a specific java interface and reveals its attributes and opterations at run time.
The JMX technology defines a connector base on RMI. The RMI connector supports the standard RMI transports, Java Remote Method Protocol(JRMP) and the Internet Inter-Lbject Request Broker(ORB) Protocol(IIOP). This connector allows you to connect to an MBean server from a remote location, and perform operations on it, exactly as if the operations were being performed locally.
due to its size, the Server.java class is shown in several code excerpts.
public static void main(String[] args) {
try {
// Firstly, the Server.java class creates a new MBean server called mbs by calling the
// createMBeanServer() method of the MBeanServerFactory class.
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
waitForEnterPressed();
// Then, the default domain in which the MBean server is registered is obtained with a call to the
// getDefaultDomain() method of the MBeanServer interface. The domain is identified by the the string domain .
String domain = mbs.getDefaultDomain();
waitForEnterPressed();
// The MBean class named SimpleStandard is also identified by a variable mbeanClassName .
// SimpleStandard is the name of java class for the java object of which this MBean is an instance.
String mbeanClassName = "SimpleStandard";
// Anothere variable, the string mbeanObjectNameStr, is defined as the combination of the domain,
// plus the following key=value pairs:
// The type, which in this case is the variable value of mbeanClassName
// A name, to differentiate this MBean from other MBeans of the same type that might be created subsequently.
// In this case the name number is 1.
// The purpose of mbeanObjectNameStr is to give the MBean a human-readble identifier.
String mbeanObjectNameStr = domain + ":type=" + mbeanClassName + ",name=1";
// A call to createSimpleMBean creates and registers the SimpleStandard MBean in the local MBean server,
// with the given object name.
ObjectName mbeanObjectName = createSimpleMBean(mbs, mbeanClassName, mbeanObjectNameStr);
waitForEnterPressed();
printMBeanInfo(mbs, mbeanObjectName, mbeanClassName);
waitForEnterPressed();
manageSimpleMBean(mbs, mbeanObjectName, mbeanClassName);
waitForEnterPressed();
// This MBean is an instance of SimpleDynamic class
mbeanClassName = "SimpleDynamic";
mbeanObjectNameStr = domain + ":type=" + mbeanClassName + ",name=1";
mbeanObjectName = createSimpleMBean(mbs, mbeanClassName, mbeanObjectNameStr);
waitForEnterPressed();
printMBeanInfo(mbs, mbeanObjectName, mbeanClassName);
waitForEnterPressed();
manageSimpleMBean(mbs, mbeanObjectName, mbeanClassName);
waitForEnterPressed();
[...]
// Create a new service URL called url, which serves as an address for the connector server.
// In this example, the service URL is given in JNDI form. This service URL defines the following:
// The connector will use default RMI transport, denoted by rmi .
// The RMI registry in which the RMI connector stub will be running on port 9999 on the local host,
// and the server address will be registered under the name server. The 9999 specified in this example is arbitrary; you can use any available port.
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
// An RMI connector server named cm is created by calling the static method
// newJMXConnectorServer of JMXConnectorFactory, with the service URL url , a null environment map,
// and the MBean server mbs as parameters.
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
// The connector server cs is launched by calling the start() method of JMXConnectorServer,
// whereupon RMIConnectorServer exports the RMI object server to the RMI registry.
// The connector will remain open until the Enter key is pressed,
// as instructed by the simple method waitForEnterPressed, that is defined later in the Server code.
cs.start();
waitForEnterPressed();
cs.stop();