Changing a dynamic object @ManagedResource dynamicName

I am prototyping beans programmatically / dynamically. I want those beans after launch to be in the jmx console. How can I distinguish between them? I use anotations to add my beans in jmx and I have

@ManagedResource(objectName="bean:name=MybBean") 

I need to dynamically enter the name of an object. Any idea how I could do this?

Here is my jmx configuration:

 <context:mbean-export server="mbeanServer" /> <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" /> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false"> <property name="beans"> <map> <entry key="Server:name=HttpAdaptor"> <bean class="mx4j.tools.adaptor.http.HttpAdaptor"> <property name="port" value="8000" /> <property name="host" value="0.0.0.0" /> <property name="processor"> <bean class="mx4j.tools.adaptor.http.XSLTProcessor" /> </property> </bean> </entry> </map> </property> <property name="listeners"> <list> <!-- --> <bean class="com.fixgw.jmx.HttpAdaptorMgr"> <property name="mbeanServer" ref="mbeanServer" /> </bean> </list> </property> </bean> <bean id="sessionMDB" class="com.fixgw.mdb.SessionMDB" scope="prototype" lazy-init="true"> <constructor-arg ref="0" /> <constructor-arg ref="0" /> </bean> 
+6
source share
2 answers

You can use the JMX naming strategy for this. At work, we use the interface:

 public interface RuntimeJmxNames { /** this is the name= part of the object name */ public String getJmxName(); /** this sets the folders as 00=FirstFolder,01=Second */ public String[] getJmxPath(); } 

I posted code to implement the RuntimeMetadataNamingStrategy naming strategy .

And here is something like the following Spring beans:

 <bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" /> <bean id="jmxAssembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler"> <property name="attributeSource" ref="jmxAttributeSource" /> </bean> <bean id="jmxNamingStrategy" class="com.j256.jmx.RuntimeMetadataNamingStrategy"> <property name="attributeSource" ref="jmxAttributeSource" /> </bean> <bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="autodetect" value="true" /> <property name="assembler" ref="jmxAssembler" /> <property name="namingStrategy" ref="jmxNamingStrategy" /> <property name="ensureUniqueRuntimeObjectNames" value="false" /> <property name="excludedBeans" ref="excludedJmxBeans" /> </bean> 

In your code, you are doing something like:

 @ManagedResource(objectName = "foo.com:name=replaced", description = "...") public class Foo implements RuntimeJmxNames { ... public String getJmxName() { // here where you can make the name be dynamic return toString(); } @Override public String[] getJmxPath() { return new String[] { "folder" }; } } 

Here's the Spring JMX naming documentation, although I'm not 100% sure it covers custom naming stuff.

Also, my SimpleJMX package does a lot of this, although its Spring support is poorly documented.

+5
source

You can do this by simply auto-enlarging the exporter and incorporating org.springframework.jmx.export.naming.SelfNaming into your prototype class.

i.e:.

  @Component("MyPrototypeScopedBeanName") @Scope(value = "prototype") @ManagedResource public class MyPrototypeScopedBeanName implements SelfNaming @Autowired MBeanExporter exporter; . . @PostConstruct private void init() throws Exception { exporter.registerManagedResource(this); } . . . @Override public ObjectName getObjectName() throws MalformedObjectNameException { return new ObjectName("com.foobar", "name", this.toString()); } 

In addition, you can configure the exporter to ignore it during auto-detection, since the autodetect method works with prototypes, it will create another instance for itself, which will add an additional instance to your JMX console.

 <property name="autodetect" value="true"/> <!-- Done to prevent creation of additional prototype during autodetect routine --> <property name="excludedBeans"> <list> <value>MyPrototypeScopedBeanName</value> </list> </property> 
+14
source

Source: https://habr.com/ru/post/920597/


All Articles