Spring与JMX集成

在csdn上看到一篇使用MBeanExporter让Spring与JMX集成的文章:
http://blog.csdn.net/shirdrn/article/details/6358688

自己也总结一下项目中的实践:

1. 扩展MBeanExporter类, 让其在初始化的时候,获得所有需要被注册为MBean的普通java bean

Spring与JMX集成
public class MetaAttributeMBeanExporter extends MBeanExporter
{
    protected ListableBeanFactory theBeanFactory;
    
    public static final String JMX_NAME = "jmxName";
    
    protected boolean initialized = false;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            this.theBeanFactory = (ListableBeanFactory) beanFactory;
        }
        else {
            logger.info("MBeanExporter not running in a ListableBeanFactory: Autodetection of MBeans not available.");
        }
    
        super.setBeanFactory(beanFactory);
    }

    @Override
    public void afterPropertiesSet() 
    {

        Map<String, Object> beanMap = new HashMap<String, Object>();
        
        if (this.server == null) {
            this.server = JmxUtils.locateMBeanServer();
        }
        
        String[] beanNames = this.theBeanFactory.getBeanNamesForType(Object.class, true, false);
        for (int i = 0; i < beanNames.length; i++) {
            String beanName = beanNames[i];
            String beanJMXName = getBeanJMXNameIfAny(theBeanFactory, beanName);
            
            
            if ( beanJMXName != null) {
                boolean isAlreadyRegistered = false;
                try
                {
                    isAlreadyRegistered = server.isRegistered(getObjectName(beanName, beanJMXName));
                }
                catch (MalformedObjectNameException e) {
                    logger.warn("The bean JMX Name [" + beanJMXName + "] is malformed. Skipping...");
                    continue;
                }
                
                if ( !isAlreadyRegistered )
                {
                    // Not already registered for JMX exposure.
                    beanMap.put(beanJMXName, beanName);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Bean with name ‘" + beanName + "‘ has been autodetected for JMX exposure");
                    }
                }
                
            }
            
        }
        
        //finally
        super.setBeans(beanMap);
        super.afterPropertiesSet();
            

    }

    protected String getBeanJMXNameIfAny(ListableBeanFactory beanFactory, String beanName) 
    {
        if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
            return null;
        }
        try {
            BeanDefinition bd = ((ConfigurableListableBeanFactory) beanFactory).getBeanDefinition(beanName);
            BeanMetadataAttributeAccessor def = (BeanMetadataAttributeAccessor) bd;
            BeanMetadataAttribute meta = def.getMetadataAttribute(JMX_NAME);
            if ( meta == null )
            {
                return null;
            }
            else
            {
                return (String)meta.getValue();
            }
        }
        catch (NoSuchBeanDefinitionException ex) {
            // Probably a directly registered singleton.
            return null;
        }
    }

}
Spring与JMX集成

2. spring bean 配置:

Spring与JMX集成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" 
   xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">    


    <!-- this bean must not be lazily initialized if the exporting is to happen -->
    <bean id="exporter" class="com.citi.ficc.jmx.MetaAttributeMBeanExporter" lazy-init="false" />

</beans>
Spring与JMX集成

3. 然后在需要注册成为MBean的普通java bean的定义里加上meta 属性:

Spring与JMX集成
<bean id="jmxutils" class="xxx">
        <meta key="jmxName" value="domain:name=MBean"/>
    </bean>
Spring与JMX集成

 

Spring与JMX集成,布布扣,bubuko.com

Spring与JMX集成

上一篇:C++ 箭头-> 双冒号:: 点号.操作符区别


下一篇:从Java到C++——数组的用法