zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    关于cocoapods中的一些报错解决方案
    基于 声网Agora SDK 实现 iOS 端的多人音视频互动
    AppStore中使用IDFA后提交应用的注意事项
    iOS打包测试和发布
    LLDB常用调试命令
    MRC与ARC区别以及五大内存区
    关于友盟分享和微信支付冲突的解决办法
    iOS开发集成支付宝支付、支付宝&微信支付
    关于iOS引导页图层的相关属性类
    遇见的奇葩问题
  • 原文地址:https://www.cnblogs.com/longzhaoyu/p/3649932.html
Copyright © 2011-2022 走看看