-
JMX ObjectName
Hi,
I am using @ManagedResurce annotation to define the POJO as a MBean and @ManagedAttribute to register attributes.
The bean is a simple POJO with getter & setter.
I specify the objectName & I am able to successfully view the MBean in JConsole.
So far so good..
Now I have a use case where I need to create unique instance of the above mbean and populate it with some statistical data.
I need to register the same pojo multiple time with unique objectName.
How can I pass dynamic value to the objectName in the annotation.
I need to display in jConsole with some logical name i.e the operationName or target method name for which it is holding the value.
For eg
Code:
@Component("dataHolder")
@ManagedResource(objectName = "TestJMX:name=JMXDataHolder,type=JMXDataHolder")
public class JMXDataHolder {
...
}
-
I have resolved this issue by using programmatic approach by passing the desired objectName while registering the bean. In my POJO which is a managed been I have kept the annotations @ManagedResource (without any options) & @ManagedAttribute as it is.
Before registering the managed bean I am creating & setting the instance of MetadataMBeanInfoAssembler(JMXAttributeSource) so that it can identify the annotation used in the associated object.
Or
You can set the AutodetectMode to AUTODETECT_ASSEMBLER. This will automatically identify the JMX related annotations used in you MBean & expose those operations & attributes.
Code:
public void registerSpringWay(JMXDataHolder holder, final ObjectName objectName) {
try{
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
exporter.setServer(getPlatFormMBeanServer());
exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ASSEMBLER);
exporter.setAssembler(new MetadataMBeanInfoAssembler(new AnnotationJmxAttributeSource()));
exporter.setRegistrationBehavior(MBeanExporter.REGISTRATION_IGNORE_EXISTING);
exporter.registerManagedResource(holder,objectName);
}catch(Exception e){
e.printStackTrace();
}
}
MBeanServer server=null;
/**
* @return
*/
private MBeanServer getPlatFormMBeanServer() {
if(null==server){
this.server= ManagementFactory.getPlatformMBeanServer();
}
return server;
}