Java Reference
In-Depth Information
ModelMBeanOperationInfo getDestDirInfo = new ModelMBeanOperationInfo(
"Get destination directory",
FileReplicator.class.getMethod("getDestDir"));
ModelMBeanOperationInfo setDestDirInfo = new ModelMBeanOperationInfo(
"Set destination directory",
FileReplicator.class.getMethod("setDestDir", String.class));
ModelMBeanOperationInfo replicateInfo = new ModelMBeanOperationInfo(
"Replicate files",
FileReplicator.class.getMethod("replicate"));
ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(
"FileReplicator", "File replicator",
new ModelMBeanAttributeInfo[] { srcDirInfo, destDirInfo },
null,
new ModelMBeanOperationInfo[] { getSrcDirInfo, setSrcDirInfo,
getDestDirInfo, setDestDirInfo, replicateInfo },
null);
mbean.setModelMBeanInfo(mbeanInfo);
mbeanServer.registerMBean(mbean, objectName);
} catch (JMException e) {
...
} catch (InvalidTargetObjectTypeException e) {
...
} catch (NoSuchMethodException e) {
...
}
System.in.read();
}
}
To register an MBean, you need an instance of the interface
javax.managment.MBeanServer
. In
JDK 1.5, you can call the static method
ManagementFactory.getPlatformMBeanServer()
to locate a
platform MBean server. It will create an MBean server if none exists, and then register this server
instance for future use. Each MBean requires an MBean
object name
that includes a domain. The
preceding MBean is registered under the domain
bean
with the name
documentReplicator
.
From the preceding code, you can see that for each MBean attribute and MBean operation, you
need to create a
ModelMBeanAttributeInfo
object and a
ModelMBeanOperationInfo
object for describing it.
After those, you have to create a
ModelMBeanInfo
object for describing the MBean's management
interface by assembling the preceding information. For details about using these classes, you can
consult their javadocs.
Moreover, you have to handle the JMX-specific exceptions when calling the JMX API. These
exceptions are checked exceptions that you must handle.
Note that you must prevent your application from terminating before you look inside it with a
JMX client tool. Requesting a key from the console using
System.in.read()
would be a good choice.
Finally, you have to add the VM argument
-Dcom.sun.management.jmxremote
to enable local
monitoring of this application. You should also include all other options for your command, such as
the classpath, as necessary.
java -Dcom.sun.management.jmxremote com.apress.
➥
springenterpriserecipes.replicator.Main
Search WWH ::
Custom Search