Tomcat的Server初始化及启动过程

Tomcat7,启动过程(BootStrap、Catalina):[url]http://donald-draper.iteye.com/blog/2326533[/url]
Tomcat的Engine初始化,启动过程:[url]http://donald-draper.iteye.com/blog/2327119[/url](看完这篇文章,再看Engine初始化,启动过程)
通过分析Tomcat的启动过程,我们可以看出Tomcat的Server的启动关键在
load方法和start方法,如下:
public class Catalina {  
protected String configFile = "conf/server.xml";
protected Server server = null;
//加载Server实例
public void load() {
getServer().setCatalina(this);
try {
//初始化Server
getServer().init();
}
public void start() {
if (getServer() == null) {
//加载Server实例
load();
}
long t1 = System.nanoTime();
try {
//启动Server
getServer().start();
}
}
}

而这两个方法都是调用的Server方法,下面来看一下StandardServer的这两个方法
//
public final class StandardServer extends LifecycleMBeanBase implements Server {
//看到一下变量,是否会想到Server.xml中的配置项
//
private javax.naming.Context globalNamingContext = null;
private NamingResources globalNamingResources = null;
private static final String info =
"org.apache.catalina.core.StandardServer/1.0";
private NamingContextListener namingContextListener = null;
//
private int port = 8005;
private String address = "localhost";
private Random random = null;

private Service services[] = new Service[0];
private final Object servicesLock = new Object();
private String shutdown = "SHUTDOWN";
private static final StringManager sm =
StringManager.getManager(Constants.Package);
PropertyChangeSupport support = new PropertyChangeSupport(this);
private volatile boolean stopAwait = false;
private Catalina catalina = null;
private ClassLoader parentClassLoader = null;
private volatile Thread awaitThread = null;
private volatile ServerSocket awaitSocket = null;
}

Catatlina的load方法实际上,是调用的Server.init方法,F3到init的定义,我们
可以看到这个方法是在Lifecycle类中
public interface Lifecycle {
//生命周期状态事件
public static final String BEFORE_INIT_EVENT = "before_init";
public static final String AFTER_INIT_EVENT = "after_init";
public static final String START_EVENT = "start";
public static final String BEFORE_START_EVENT = "before_start";
public static final String AFTER_START_EVENT = "after_start";
public static final String STOP_EVENT = "stop";
public static final String BEFORE_STOP_EVENT = "before_stop";
public static final String AFTER_STOP_EVENT = "after_stop";
public static final String AFTER_DESTROY_EVENT = "after_destroy";
public static final String BEFORE_DESTROY_EVENT = "before_destroy";
public static final String PERIODIC_EVENT = "periodic";
public static final String CONFIGURE_START_EVENT = "configure_start";
public static final String CONFIGURE_STOP_EVENT = "configure_stop";
//留给子类扩展
public void init() throws LifecycleException;
}

实际上Tomcat容器中Server,Service,Container(Engine,HOST),Connector的生命周期都是通过Lifecycle
来管理,StandardServer extends LifecycleMBeanBase,我们来查看一下
//LifecycleMBeanBase,JMX管理MBeanServer
public abstract class LifecycleMBeanBase extends LifecycleBase{
private String domain = null;
private ObjectName oname = null;
protected MBeanServer mserver = null;
@Override
//注册Mbean到JMX
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());
}
}
}

再查看LifecycleBase
public abstract class LifecycleBase implements Lifecycle {
@Override
public final synchronized void init() throws LifecycleException {
if (!state.equals(LifecycleState.NEW)) {
invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
}
setStateInternal(LifecycleState.INITIALIZING, null, false);
try {
//这个就是我们要找的Server,load过程的切入点;
initInternal();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
setStateInternal(LifecycleState.FAILED, null, false);
throw new LifecycleException(
sm.getString("lifecycleBase.initFail",toString()), t);
}
setStateInternal(LifecycleState.INITIALIZED, null, false);
}
//留给子类扩展
protected abstract void initInternal() throws LifecycleException;
}

//来看一下LifecycleState
public enum LifecycleState {
NEW(false, null),
INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT),
INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT),
STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT),
STARTING(true, Lifecycle.START_EVENT),
STARTED(true, Lifecycle.AFTER_START_EVENT),
STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT),
STOPPING(false, Lifecycle.STOP_EVENT),
STOPPED(false, Lifecycle.AFTER_STOP_EVENT),
DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT),
DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT),
FAILED(false, null),
MUST_STOP(true, null),
MUST_DESTROY(false, null);
private final boolean available;
private final String lifecycleEvent;

private LifecycleState(boolean available, String lifecycleEvent) {
this.available = available;
this.lifecycleEvent = lifecycleEvent;
}
public boolean isAvailable() {
return available;
}
public String getLifecycleEvent() {
return lifecycleEvent;
}
}

从LifecycleBase的init方法可以看出,init方法实际上,调用的是initInternal方法,
而这个方法为抽象,待子类去扩展,而生命周期的状态是通过LifecycleState( ENUM )
枚举类描述。
现在回到Server的initInternal的方法:
//
public final class StandardServer extends LifecycleMBeanBase implements Server {
@Override
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");
//注册MBeanFactory
MBeanFactory factory = new MBeanFactory();
factory.setContainer(this);
onameMBeanFactory = register(factory, "type=MBeanFactory");
//注册全局命名资源
globalNamingResources.init();
// Populate the extension validator with JARs from common and shared
// class loaders
if (getCatalina() != null) {
ClassLoader cl = getCatalina().getParentClassLoader();
// Walk the class loader hierarchy. Stop at the system class loader.
// This will add the shared (if present) and common class loaders
while (cl != null && cl != ClassLoader.getSystemClassLoader()) {
if (cl instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) cl).getURLs();
for (URL url : urls) {
if (url.getProtocol().equals("file")) {
try {
File f = new File (url.toURI());
if (f.isFile() &&
f.getName().endsWith(".jar")) {
ExtensionValidator.addSystemResource(f);
}
}
}
}
}
cl = cl.getParent();
}
}
// 初始化Service
for (int i = 0; i < services.length; i++) {
// 初始化Service
services[i].init();
}
}
}

//下面来分析 getServer().start(),Server的start方法与init的方法类似,定义在
Lifecycle,实现在LifecycleBase,我们来看LifecycleBase
public abstract class LifecycleBase implements Lifecycle {
@Override
public final synchronized void start() throws LifecycleException {
if (LifecycleState.STARTING_PREP.equals(state) || LifecycleState.STARTING.equals(state) ||
LifecycleState.STARTED.equals(state)) {
if (log.isDebugEnabled()) {
Exception e = new LifecycleException();
log.debug(sm.getString("lifecycleBase.alreadyStarted", toString()), e);
} else if (log.isInfoEnabled()) {
log.info(sm.getString("lifecycleBase.alreadyStarted", toString()));
}
return;
}
if (state.equals(LifecycleState.NEW)) {
init();
} else if (state.equals(LifecycleState.FAILED)) {
stop();
} else if (!state.equals(LifecycleState.INITIALIZED) &&
!state.equals(LifecycleState.STOPPED)) {
invalidTransition(Lifecycle.BEFORE_START_EVENT);
}
setStateInternal(LifecycleState.STARTING_PREP, null, false);
try {
//关键切入点
startInternal();
}
if (state.equals(LifecycleState.FAILED)) {
stop();
} else if (!state.equals(LifecycleState.STARTING)) {
invalidTransition(Lifecycle.AFTER_START_EVENT);
} else {
setStateInternal(LifecycleState.STARTED, null, false);
}
}
//留给父类扩展
protected abstract void startInternal() throws LifecycleException;
}

再回到现在回到Server的startInternal的方法:
//
public final class StandardServer extends LifecycleMBeanBase implements Server {
@Override
protected void startInternal() throws LifecycleException {
//触发CONFIGURE_START_EVENT事件
fireLifecycleEvent(CONFIGURE_START_EVENT, null);
//设置状态为开始
setState(LifecycleState.STARTING);
globalNamingResources.start();
// Start our defined Services
synchronized (servicesLock) {
for (int i = 0; i < services.length; i++) {
//启动Service
services[i].start();
}
}
}
}

我们再来看一下StandardServer的addService方法
//添加Service
@Override
public void addService(Service service) {
//将Service与Server关联
service.setServer(this);
synchronized (servicesLock) {
//下面几行代码很有意义,用的是数组而不是 List 集合,
//这个从性能角度考虑可以理解,有趣的是这里用了数组但是并没有向我们平常那样,
//一开始就分配一个固定大小的数组,
//它这里的实现机制是:重新创建一个当前大小的数组对象,
//然后将原来的数组对象 copy 到新的数组中,这种方式实现了类似的动态数组的功能,
//这种实现方式,值得我们以后拿来借鉴。
Service results[] = new Service[services.length + 1];
System.arraycopy(services, 0, results, 0, services.length);
results[services.length] = service;
services = results;
if (getState().isAvailable()) {
try {
//启动service
service.start();
}
}
// Report this property change to interested listeners
support.firePropertyChange("service", null, service);
}
}

至此,Server的init和Start方法已经分析完,下面我们来看一下StandardService,从以上的分析,
我们只需要查看initInternal和startInternal即可
//
public class StandardService extends LifecycleMBeanBase implements Service {
private static final String info =
"org.apache.catalina.core.StandardService/1.0";
//
private String name = null;
private static final StringManager sm =
StringManager.getManager(Constants.Package);
private Server server = null;
protected PropertyChangeSupport support = new PropertyChangeSupport(this);
/* connectionTimeout="20000"
redirectPort="8443" />






*/
protected Connector connectors[] = new Connector[0];
private final Object connectorsLock = new Object();
/*
*/
//线程执行器
protected ArrayList executors = new ArrayList();
/*






resourceName="UserDatabase"/>

unpackWARs="true" autoDeploy="true">
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />


*/
//Engine,Host,Realm,都是Container
protected Container container = null;
private ClassLoader parentClassLoader = null;

@Override
protected void initInternal() throws LifecycleException {
//注册Service到JMX
super.initInternal();
//初始化Container
if (container != null) {
container.init();
}
//初始化Executors
for (Executor executor : findExecutors()) {
if (executor instanceof LifecycleMBeanBase) {
((LifecycleMBeanBase) executor).setDomain(getDomain());
}
executor.init();
}
synchronized (connectorsLock) {
for (Connector connector : connectors) {
try {
//初始化Connector
connector.init();
}
}
}
}
@Override
protected void startInternal() throws LifecycleException {
if(log.isInfoEnabled())
log.info(sm.getString("standardService.start.name", this.name));
//设置Service状态
setState(LifecycleState.STARTING);
if (container != null) {
synchronized (container) {
//启动container
container.start();
}
}
synchronized (executors) {
for (Executor executor: executors) {
//启动executor
executor.start();
}
}
synchronized (connectorsLock) {
for (Connector connector: connectors) {
try {
if (connector.getState() != LifecycleState.FAILED) {
//启动connector
connector.start();
}
}
}
}
}

* Set the Container that handles requests for all
* Connectors associated with this Service.
/*


*/
//Container处理所有请求
public void setContainer(Container container) {
Container oldContainer = this.container;
if ((oldContainer != null) && (oldContainer instanceof Engine))
((Engine) oldContainer).setService(null);
this.container = container;
if ((this.container != null) && (this.container instanceof Engine))
((Engine) this.container).setService(this);
if (getState().isAvailable() && (this.container != null)) {
try {
//启动Engine
this.container.start();
}
}
if (getState().isAvailable() && (oldContainer != null)) {
try {
oldContainer.stop();
}
}
// Report this property change to interested listeners
support.firePropertyChange("container", oldContainer, this.container);

}
//添加Connector
public void addConnector(Connector connector) {
synchronized (connectorsLock) {
//这个与Server.addService一样的理念
connector.setService(this);
Connector results[] = new Connector[connectors.length + 1];
System.arraycopy(connectors, 0, results, 0, connectors.length);
results[connectors.length] = connector;
connectors = results;
if (getState().isAvailable()) {
try {
connector.start();
}
}
// Report this property change to interested listeners
support.firePropertyChange("connector", null, connector);
}
}
//添加Executor
public void addExecutor(Executor ex) {
synchronized (executors) {
if (!executors.contains(ex)) {
executors.add(ex);
if (getState().isAvailable())
try {
ex.start();
}
}
}
}
}

//Constants
public class Constants {
public static final String Package = "org.apache.catalina.core";
public static final int MAJOR_VERSION = 3;
public static final int MINOR_VERSION = 0;
public static final String JSP_SERVLET_CLASS =
"org.apache.jasper.servlet.JspServlet";
}

从分析StandardService可以看出StandardService主要组成部分为Container(Engine)
,Executors,Connectors,initInternal与startInternal分别初始化和启动Container(Engine),Executors,Connectors。
总结[color=blue]:实际上Tomcat容器中Server,Service,Container(Engine,HOST),Connector的生命周期都是通过Lifecycle来管理,Catalina依托于Server,而Server的服务对象为Service;Service的主要组成部分为Container(Engine),Executors,Connectors,Server的初始化与启动实际上,为初始化和启动Container(Engine),Executors,Connectors,下面的章节,我们分别来讲Container(Engine),Executors,Connectors[/color]

你可能感兴趣的:(Tomcat)