我想创建一个 JMX 客户端,它可以与我的服务器一起使用。我可以使用 jconsole 连接到我的服务器,但是如何使用 java 代码来连接。
这是我的服务器代码:
MBeanServer mbeanServer = ManagementFactory
.getPlatformMBeanServer();
//giving name for service
ObjectName objectName = new ObjectName("bean:name=logService");
RequiredModelMBean mbean = new RequiredModelMBean();
mbean.setManagedResource(logService, "objectReference");
Descriptor logStringDescriptor = new DescriptorSupport(
new String[] { "name=logString",
"descriptorType=attribute",
"getMethod=getLogString", "setMethod=setLogString" });
ModelMBeanAttributeInfo logStringAttribute = new ModelMBeanAttributeInfo(
"logString", "java.lang.String", "String to be logged",
true, true, false, logStringDescriptor);
ModelMBeanOperationInfo getLogStringOperation = new ModelMBeanOperationInfo(
"Get the log string",
LoggerService.class.getMethod("getLogString"));
ModelMBeanOperationInfo setLogStringOperation = new ModelMBeanOperationInfo(
"Set the log string", LoggerService.class.getMethod(
"setLogString", String.class));
ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(
"LoggerService", "Logger Service",
new ModelMBeanAttributeInfo[] { logStringAttribute }, null,
new ModelMBeanOperationInfo[] { getLogStringOperation,
setLogStringOperation }, null);
mbean.setModelMBeanInfo(mbeanInfo);
mbeanServer.registerMBean(mbean, objectName);