下面列出了怎么用javax.management.modelmbean.RequiredModelMBean的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testRegisterandUnregister() throws JMException {
reset();
// Register MBean and return different ObjectName
when(mbeanServer.isRegistered(sourceObjectName)).thenReturn(false);
when(mbeanServer.registerMBean(any(RequiredModelMBean.class), any(ObjectName.class))).thenReturn(instance);
when(instance.getObjectName()).thenReturn(registeredObjectName);
when(mbeanServer.isRegistered(registeredObjectName)).thenReturn(true);
agent.register(object, sourceObjectName);
verify(mbeanServer).isRegistered(sourceObjectName);
verify(mbeanServer).registerMBean(any(RequiredModelMBean.class), any(ObjectName.class));
assertTrue(agent.isRegistered(sourceObjectName));
agent.unregister(sourceObjectName);
verify(mbeanServer).unregisterMBean(registeredObjectName);
assertFalse(agent.isRegistered(sourceObjectName));
}
@Test
public void testRegisterandUnregister() throws JMException {
reset();
// Register MBean and return different ObjectName
when(mbeanServer.isRegistered(sourceObjectName)).thenReturn(false);
when(mbeanServer.registerMBean(any(RequiredModelMBean.class), any(ObjectName.class))).thenReturn(instance);
when(instance.getObjectName()).thenReturn(registeredObjectName);
when(mbeanServer.isRegistered(registeredObjectName)).thenReturn(true);
agent.register(object, sourceObjectName);
verify(mbeanServer).isRegistered(sourceObjectName);
verify(mbeanServer).registerMBean(any(RequiredModelMBean.class), any(ObjectName.class));
assertTrue(agent.isRegistered(sourceObjectName));
agent.unregister(sourceObjectName);
verify(mbeanServer).unregisterMBean(registeredObjectName);
assertFalse(agent.isRegistered(sourceObjectName));
}
/**
* Hooks an {@link nl.nn.adapterframework.core.Adapter Adapter} to the MBean server
*/
public static void hookupAdapter(IAdapter adapter) throws ConfigurationException {
MBeanServer server = getMBeanServer();
if (server != null) {
synchronized(registeredAdapters) {
try {
ObjectName objectName = getObjectName(adapter);
RequiredModelMBean modelMbean =
new RequiredModelMBean(createMBeanInfo(adapter));
modelMbean.setManagedResource(adapter, "ObjectReference");
LOG.debug("modelMBean generated for object " + objectName);
ObjectInstance objectInstance = registerMBean(server, objectName, modelMbean);
if (objectInstance!=null) {
registeredAdapters.put(adapter, objectInstance.getObjectName());
}
} catch (Exception e) {
throw new ConfigurationException(e);
}
}
} else {
LOG.warn("No MBean server found");
}
}
/**
* Registers an mBean at an MBeanServer. If there is already an mbean registered
* under the specified name, it is first de-registered.
*/
private static ObjectInstance registerMBean(MBeanServer server, ObjectName name, RequiredModelMBean mbean) throws ConfigurationException {
try {
if (server.isRegistered(name)) {
LOG.debug("unregistering [" + name.getCanonicalName() + "] as it already exists");
server.unregisterMBean(name);
}
ObjectInstance objectInstance = server.registerMBean(mbean, name);
LOG.debug("MBean [" + name.getCanonicalName() + "] registered as [" + objectInstance.getObjectName().getCanonicalName()+"]");
return objectInstance;
} catch (InstanceAlreadyExistsException iae) {
LOG.warn("Could not register mbean [" + name.getCanonicalName() + "]:" + iae.getMessage());
} catch (Exception e) {
throw new ConfigurationException(e);
}
return null;
}
@Test
public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception {
ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");
ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null);
RequiredModelMBean bean = new RequiredModelMBean(info);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(getServer());
exporter.registerManagedResource(bean, objectName);
MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName);
assertEquals(info, infoFromServer);
}
@Test
public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception {
ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");
ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null);
RequiredModelMBean bean = new RequiredModelMBean(info);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(getServer());
exporter.registerManagedResource(bean, objectName);
MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName);
assertEquals(info, infoFromServer);
}
public static void main(String[] args) throws Exception {
// 获取当前平台 MBean Server
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
RequiredModelMBean modelMBean = new RequiredModelMBean();
modelMBean.setManagedResource(new User(), "objectReference");
// 创建 RequiredModelMBean 的 ObjectName
ObjectName objectName = new ObjectName("thinking.in.spring.boot.samples.production.ready.jmx:type=User");
mBeanServer.registerMBean(modelMBean, objectName);
System.out.printf("MBean (ObjectName[%s])已注册到平台 MBean Server...\n", objectName);
System.out.println("按任意键结束...");
System.in.read();
}
public ModelMBean assemble(Object obj, ObjectName name) throws JMException {
ModelMBeanInfo mbi = null;
// use the default provided mbean which has been annotated with JMX
// annotations
LOG.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
mbi = assembler.getMBeanInfo(obj, null, name.toString());
if (mbi == null) {
return null;
}
RequiredModelMBean mbean = new RequiredModelMBean(mbi);
try {
mbean.setManagedResource(obj, "ObjectReference");
} catch (InvalidTargetObjectTypeException e) {
throw new JMException(e.getMessage());
}
// Allows the managed object to send notifications
if (obj instanceof NotificationSenderAware) {
((NotificationSenderAware) obj).setNotificationSender(new NotificationSenderAdapter(mbean));
}
return mbean;
}
@Test
public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception {
ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");
ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null);
RequiredModelMBean bean = new RequiredModelMBean(info);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(getServer());
exporter.registerManagedResource(bean, objectName);
MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName);
assertEquals(info, infoFromServer);
}
@Override
public ModelMBean assemble(Object obj, ObjectName name) throws JMException {
ModelMBeanInfo mbi = null;
// use the default provided mbean which has been annotated with JMX annotations
LOGGER.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
mbi = assembler.getMBeanInfo(obj, null, name.toString());
if (mbi == null) {
return null;
}
RequiredModelMBean mbean = new RequiredModelMBean(mbi);
try {
mbean.setManagedResource(obj, "ObjectReference");
} catch (InvalidTargetObjectTypeException e) {
throw new JMException(e.getMessage());
}
// Allows the managed object to send notifications
if (obj instanceof NotificationSenderAware) {
((NotificationSenderAware) obj).setNotificationSender(new NotificationSenderAdapter(mbean));
}
return mbean;
}
void initialise(RMConfiguration config, Conduit c, EndpointReferenceType r,
org.apache.cxf.transport.Destination d,
Message message) {
configuration = config;
conduit = c;
replyTo = r;
createServices();
createEndpoints(d);
setPolicies(message);
if (manager != null && manager.getBus() != null) {
managedEndpoint = new ManagedRMEndpoint(this);
instrumentationManager = manager.getBus().getExtension(InstrumentationManager.class);
if (instrumentationManager != null) {
ModelMBeanAssembler assembler = new ModelMBeanAssembler();
ModelMBeanInfo mbi = assembler.getModelMbeanInfo(managedEndpoint.getClass());
MBeanServer mbs = instrumentationManager.getMBeanServer();
if (mbs == null) {
LOG.log(Level.WARNING, "MBeanServer not available.");
} else {
try {
RequiredModelMBean rtMBean =
(RequiredModelMBean)mbs.instantiate("javax.management.modelmbean.RequiredModelMBean");
rtMBean.setModelMBeanInfo(mbi);
try {
rtMBean.setManagedResource(managedEndpoint, "ObjectReference");
} catch (InvalidTargetObjectTypeException itotex) {
throw new JMException(itotex.getMessage());
}
ObjectName name = managedEndpoint.getObjectName();
instrumentationManager.register(rtMBean, name);
modelMBean = rtMBean;
} catch (JMException jmex) {
LOG.log(Level.WARNING, "Registering ManagedRMEndpoint failed.", jmex);
}
}
}
}
}
private void register(Object obj, ObjectName name, ModelMBeanInfo mbi, boolean forceRegistration)
throws JMException {
RequiredModelMBean rtMBean =
(RequiredModelMBean)mbs.instantiate("javax.management.modelmbean.RequiredModelMBean");
rtMBean.setModelMBeanInfo(mbi);
try {
rtMBean.setManagedResource(obj, "ObjectReference");
} catch (InvalidTargetObjectTypeException itotex) {
throw new JMException(itotex.getMessage());
}
registerMBeanWithServer(rtMBean, persist(name), forceRegistration);
}
/**
* Registers an mBean at an MBeanServer. If there is already an mbean registered
* under the specified name, it is first de-registered.
* @param name the objectName
* @param mbean the modelMbean to register
*/
public static void registerMBean(ObjectName name, RequiredModelMBean mbean) throws Exception {
MBeanServer server = getMBeanServer();
// if (server.isRegistered(name)) {
// log.debug("unregistering ["+name.getCanonicalName()+"] as it already exists");
// server.unregisterMBean(name);
// }
server.registerMBean(mbean, name);
log.debug("MBean [" + name.getCanonicalName() + "] registered");
return;
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
/**
* Create an instance of a class that implements {@code ModelMBean}.
* <p>This method is called to obtain a {@code ModelMBean} instance to
* use when registering a bean. This method is called once per bean during the
* registration phase and must return a new instance of {@code ModelMBean}
* @return a new instance of a class that implements {@code ModelMBean}
* @throws javax.management.MBeanException if creation of the ModelMBean failed
*/
protected ModelMBean createModelMBean() throws MBeanException {
return (this.exposeManagedResourceClassLoader ? new SpringModelMBean() : new RequiredModelMBean());
}
/**
* Create an instance of a class that implements {@code ModelMBean}.
* <p>This method is called to obtain a {@code ModelMBean} instance to
* use when registering a bean. This method is called once per bean during the
* registration phase and must return a new instance of {@code ModelMBean}
* @return a new instance of a class that implements {@code ModelMBean}
* @throws javax.management.MBeanException if creation of the ModelMBean failed
*/
protected ModelMBean createModelMBean() throws MBeanException {
return (this.exposeManagedResourceClassLoader ? new SpringModelMBean() : new RequiredModelMBean());
}
/**
* Create an instance of a class that implements {@code ModelMBean}.
* <p>This method is called to obtain a {@code ModelMBean} instance to
* use when registering a bean. This method is called once per bean during the
* registration phase and must return a new instance of {@code ModelMBean}
* @return a new instance of a class that implements {@code ModelMBean}
* @throws javax.management.MBeanException if creation of the ModelMBean failed
*/
protected ModelMBean createModelMBean() throws MBeanException {
return (this.exposeManagedResourceClassLoader ? new SpringModelMBean() : new RequiredModelMBean());
}
/**
* Create an instance of a class that implements {@code ModelMBean}.
* <p>This method is called to obtain a {@code ModelMBean} instance to
* use when registering a bean. This method is called once per bean during the
* registration phase and must return a new instance of {@code ModelMBean}
* @return a new instance of a class that implements {@code ModelMBean}
* @throws javax.management.MBeanException if creation of the ModelMBean failed
*/
protected ModelMBean createModelMBean() throws MBeanException {
return (this.exposeManagedResourceClassLoader ? new SpringModelMBean() : new RequiredModelMBean());
}