Spring与JMX集成

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

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

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

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;
        }
    }

}

2. spring bean 配置:

<?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>

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

<bean id="jmxutils" class="xxx">
        <meta key="jmxName" value="domain:name=MBean"/>
    </bean>

 

Spring与JMX集成,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。