tomcat 7 源码分析-5 server初始化中的JMX(DynamicMBean)

tomcat 7 源码分析-5 server初始化中的JMX(DynamicMBean)

Server的中的初始化基本核心在StandardServer中,下面的代码

protected
 
void
 initInternal() 
throws
 LifecycleException {  



      


    super
.initInternal();  



  


    // Register global String cache
  



    // Note although the cache is global, if there are multiple Servers
  



    // present in the JVM (may happen when embedding) then the same cache
  



    // will be registered under multiple names
  



    onameStringCache = register(new
 StringCache(), 
"type=StringCache"
);  



  


    // Register the MBeanFactory
  



    onameMBeanFactory = register(new
 MBeanFactory(), 
"type=MBeanFactory"
);  



      


    // Register the naming resources
  



    onameNamingResoucres = register(globalNamingResources,  


            "type=NamingResources"
);  



      


    // Initialize our defined Services
  



    for
 (
int
 i = 
0
; i < services.length; i++) {  



        services[i].init();  


    }  


}  
protected void initInternal() throws LifecycleException {
        
        super.initInternal();

        // Register global String cache
        // Note although the cache is global, if there are multiple Servers
        // present in the JVM (may happen when embedding) then the same cache
        // will be registered under multiple names
        onameStringCache = register(new StringCache(), "type=StringCache");

        // Register the MBeanFactory
        onameMBeanFactory = register(new MBeanFactory(), "type=MBeanFactory");
        
        // Register the naming resources
        onameNamingResoucres = register(globalNamingResources,
                "type=NamingResources");
        
        // Initialize our defined Services
        for (int i = 0; i < services.length; i++) {
            services[i].init();
        }
    }

 先看super.initInternal();

protected
 
void
 initInternal() 
throws
 LifecycleException {  



      


    // If oname is not null then registration has already happened via
  



    // preRegister().
  



    if
 (oname == 
null
) {  



        mserver = Registry.getRegistry(null
, 
null
).getMBeanServer();  



          


        oname = register(this
, getObjectNameKeyProperties());  



    }  


}  
protected void initInternal() throws LifecycleException {
        
        // If oname is not null then registration has already happened via
        // preRegister().
        if (oname == null) {
            mserver = Registry.getRegistry(null, null).getMBeanServer();
            
            oname = register(this, getObjectNameKeyProperties());
        }
    }

 看来问题的关键就在register这个函数了。

protected
 
final
 ObjectName register(Object obj,  



        String objectNameKeyProperties) {  


      


    // Construct an object name with the right domain
  



    StringBuilder name = new
 StringBuilder(getDomain());  



    name.append(':'
);  



    name.append(objectNameKeyProperties);  


  


    ObjectName on = null
;  



  


    try
 {  



        on = new
 ObjectName(name.toString());  



          


        Registry.getRegistry(null
, 
null
).registerComponent(obj, on, 
null
);  



    } catch
 (MalformedObjectNameException e) {  



        log.warn(sm.getString("lifecycleMBeanBase.registerFail"
, obj, name),  



                e);  


    } catch
 (Exception e) {  



        log.warn(sm.getString("lifecycleMBeanBase.registerFail"
, obj, name),  



                e);  


    }  


  


    return
 on;  



}  
protected final ObjectName register(Object obj,
            String objectNameKeyProperties) {
        
        // Construct an object name with the right domain
        StringBuilder name = new StringBuilder(getDomain());
        name.append(':');
        name.append(objectNameKeyProperties);

        ObjectName on = null;

        try {
            on = new ObjectName(name.toString());
            
            Registry.getRegistry(null, null).registerComponent(obj, on, null);
        } catch (MalformedObjectNameException e) {
            log.warn(sm.getString("lifecycleMBeanBase.registerFail", obj, name),
                    e);
        } catch (Exception e) {
            log.warn(sm.getString("lifecycleMBeanBase.registerFail", obj, name),
                    e);
        }

        return on;
    }

 接下去看registerComponent函数

public
 
void
 registerComponent(Object bean, ObjectName oname, String type)  



          throws
 Exception  



   {  


       if
( log.isDebugEnabled() ) {  



           log.debug( "Managed= "
+ oname);  



       }  


  


       if
( bean ==
null
 ) {  



           log.error("Null component "
 + oname );  



           return
;  



       }  


  


       try
 {  



           if
( type==
null
 ) {  



               type=bean.getClass().getName();  


           }  


  


           ManagedBean managed = findManagedBean(bean.getClass(), type);  


  


           // The real mbean is created and registered
  



           DynamicMBean mbean = managed.createMBean(bean);  


  


           if
(  getMBeanServer().isRegistered( oname )) {  



               if
( log.isDebugEnabled()) {  



                   log.debug("Unregistering existing component "
 + oname );  



               }  


               getMBeanServer().unregisterMBean( oname );  


           }  


  


           getMBeanServer().registerMBean( mbean, oname);  


       } catch
( Exception ex) {  



           log.error("Error registering "
 + oname, ex );  



           throw
 ex;  



       }  


   }  
public void registerComponent(Object bean, ObjectName oname, String type)
           throws Exception
    {
        if( log.isDebugEnabled() ) {
            log.debug( "Managed= "+ oname);
        }

        if( bean ==null ) {
            log.error("Null component " + oname );
            return;
        }

        try {
            if( type==null ) {
                type=bean.getClass().getName();
            }

            ManagedBean managed = findManagedBean(bean.getClass(), type);

            // The real mbean is created and registered
            DynamicMBean mbean = managed.createMBean(bean);

            if(  getMBeanServer().isRegistered( oname )) {
                if( log.isDebugEnabled()) {
                    log.debug("Unregistering existing component " + oname );
                }
                getMBeanServer().unregisterMBean( oname );
            }

            getMBeanServer().registerMBean( mbean, oname);
        } catch( Exception ex) {
            log.error("Error registering " + oname, ex );
            throw ex;
        }
    }

 终于知道这一段代码要完成什么功能了,原来register就是将参数中的object注册到MBeanServer中去,而且使用的是DynamicMBean。

Tomcat中

BaseModelMBean实现了DynamicMBean接口,是真正意思上的动态Mbeans。

ManagedBean是对MBeans的管理,例如生产Mbean等。

Registry是又一层次的封装,是将Object(Mbeans)注册到MBeanServer的一个接口,同时还提供对已有Mbeans的查找,防止重复注册等。

说到这里,如果不了解MBean的话,肯定不知道在说什么。所以我下一个续,先从简单的Standard MBeans说起。

相关推荐