Change @ManagedResource objectName dynamically

后端 未结 2 2067
囚心锁ツ
囚心锁ツ 2021-01-02 10:45

I am creating prototype beans programatically/dynamically. I want those beans after initiation to be in the jmx console. How I can distinguish between them? I am using anota

2条回答
  •  渐次进展
    2021-01-02 11:06

    You can use a a JMX naming strategy to do this. At work we use an 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've posted the code to implement the RuntimeMetadataNamingStrategy naming strategy.

    And then something like the following Spring beans:

    
    
    
        
    
    
    
        
    
    
    
        
        
        
        
        
    
    

    In your code you do something like:

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

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

    Also, my SimpleJMX package does a this as well. It uses a JmxSelfNaming interface which allows each instance of an object to define it's own bean-name to make them unique and works well with Spring.

提交回复
热议问题