您的位置:首页 > 运维架构 > Tomcat

tomcat解析(九)StandardServer.initialize.start

2010-01-27 12:58 274 查看
在前几篇文章中已说到整个服务器的启动已托管给该类的initialize及start方法,下面我们来看一下这两个方法的内容

一.initialize

/**
* Invoke a pre-startup initialization. This is used to allow connectors
* to bind to restricted ports under Unix operating environments.
*/
public void initialize()
throws LifecycleException
{
if (initialized) {
log.info(sm.getString("standardServer.initialize.initialized"));
return;
}
lifecycle.fireLifecycleEvent(INIT_EVENT, null);
initialized = true;
if( oname==null ) {
try {
oname=new ObjectName( "Catalina:type=Server");
Registry.getRegistry(null, null)
.registerComponent(this, oname, null );
} catch (Exception e) {
log.error("Error registering ",e);
}
}

// Register global String cache
try {
ObjectName oname2 =
new ObjectName(oname.getDomain() + ":type=StringCache");
Registry.getRegistry(null, null)
.registerComponent(new StringCache(), oname2, null );
} catch (Exception e) {
log.error("Error registering ",e);
}
// Initialize our defined Services
for (int i = 0; i < services.length; i++) {
services[i].initialize();
}
}


内容有三

1.lifecycle.fireLifecycleEvent(INIT_EVENT, null);

触发Lifecycle事件,将调用其LifecycleListener的相应方法,这里需要在XML文件里有配置,这里不细讲了

2.Registry.getRegistry(null, null).registerComponent(this, oname, null );

将该实例注册到MBeanFactory

3.services[i].initialize()

这里又将初始化的动作妥托给StandardService类的initialize进行

二.start()

/**
* Prepare for the beginning of active use of the public methods of this
* component.  This method should be called before any of the public
* methods of this component are utilized.  It should also send a
* LifecycleEvent of type START_EVENT to any registered listeners.
*
* @exception LifecycleException if this component detects a fatal error
*  that prevents this component from being used
*/
public void start() throws LifecycleException {
// Validate and update our current component state
if (started) {
log.debug(sm.getString("standardServer.start.started"));
return;
}
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
// Start our defined Services
synchronized (services) {
for (int i = 0; i < services.length; i++) {
if (services[i] instanceof Lifecycle)
((Lifecycle) services[i]).start();
}
}
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
}


内容有三

1.lifecycle.fireLifecycleEvent

触发Lifecycle.BEFORE_START_EVENT及Lifecycle.START_EVENT

2.((Lifecycle) services[i]).start();

妥拒给StandardService.start

3.lifecycle.fireLifecycleEvent

触发Lifecycle.AFTER_START_EVENT

StandardServer的默认Listener有如下几个

org.apache.catalina.core.AprLifecycleListener

org.apache.catalina.core.JasperListener

org.apache.catalina.mbeans.ServerLifecycleListener

org.apache.catalina.mbeans.GlobalResourcesLifecycleListener

这几个监听器的工作无非就是初化一些类如JspRuntimeContext,将之前解析XML得到的对象以MBean方式进行注册及取消注册等

下一篇我们分别来看StandardService.initialize,start
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐