Tomcat由多个组件组成,那么Tomcat是怎么对他们的生命周期进行管理的么,这里将从Tomcat源码去分析其生命周期的实现;

Bootstrape类为Tomcat的入口,所有的组件够通过实现Lifecycle接口来管理生命周期,Tomcat启动的时候只需调用Server容器的start(),然后父容器依序启动他所包含的子容器,关闭也是如此。

通过阅读源码可知一个Server里包含一个或多个Service,一个Service里包含一个Container,一个或多个Connector、Container又包含了Engine、Host、Context、Wrapper四个容器;

Tomcat的组件启动顺序为:

StandardServer.start()——》StandardServer.startInternal() ——》StandardService().start()——StandardService.startInternal() ——>》StandardEngine().start() ——》StandardEngine.startInternal()—》StandardEngine中启动其他组件,组件关闭也是如此;

现在我们通过Demo简单模拟Tomcat的启动

模拟Demo UML类图

模拟Demo时序图

主要代码段如下:

Catalina类:

package co.solinx.Pattern.Observer;

/**
* Created by LX on 2014/11/26.
*/
public class Catalina { public static void main(String[] args) { //Tomcat为多个组件组成的server为最外围最大的一个
StandardServer server = new StandardServer();
//为server添加监听器
server.addLifecycleListener(new ContextConfig());
//添加一个service
StandardService service = new StandardService();
server.AddService(service);
//为service添加监听器
service.addLifecycleListener(new ContextConfig());
//添加一个engine
StandardEngine standardEngine = new StandardEngine();
//为engine添加监听器
standardEngine.addLifecycleListener(new EngineConfig());
StandardHost standardHost = new StandardHost("localhost");
// StandardHost testHost = new StandardHost("test");
// standardHost.addLifecycleListener(new EngineConfig());
standardEngine.addChild("localhost", standardHost);
// standardEngine.addChild("test", testHost);
//往service添加engine容器
service.setContainer(standardEngine);
try {
server.start();
} catch (LifecycleException e) {
e.printStackTrace();
}
} }

StandardServer类

package co.solinx.Pattern.Observer;

/**
* Created by LX on 2014/11/26.
*/
public class StandardServer extends LifecycleBase implements Context {
Service services[] = new Service[0];
@Override
protected void startInternal() throws LifecycleException { for (int i = 0; i < services.length; i++) {
services[i].start();
} System.out.println("StandardServer start");
}
public void AddService(Service service) {
Service result[] = new Service[services.length + 1];
System.arraycopy(services, 0, result, 0, services.length);
result[services.length] = service;
services = result;
}
}

StandardService类:

package co.solinx.Pattern.Observer;

/**
* Created by LX on 2014/11/26.
*/
public class StandardService extends LifecycleBase implements Service, Context { protected ContainerBase container = null; @Override
protected void startInternal() throws LifecycleException { container.start();
System.out.println("StandardService start");
} public void setContainer(ContainerBase container) {
this.container = container;
} }

StandardEngine类:

package co.solinx.Pattern.Observer;

/**
* Created by LX on 2014/11/26.
*/
public class StandardEngine extends ContainerBase {
@Override
protected void startInternal() throws LifecycleException {
super.startInternal();
System.out.println("StandardEngine start");
} protected void addChild(String key, Container container) {
super.addChild(key, container);
}
}

LifecycleSupport类:

package co.solinx.Pattern.Observer;

/**
* Created by LX on 2014/11/26.
* 代理了具体监听者
*/
public class LifecycleSupport {
public LifecycleSupport(Lifecycle lifecycle) {
super();
this.lifecycle = lifecycle;
}
private Lifecycle lifecycle = null;
private LifecycleListener listeners[] = new LifecycleListener[0];
private final Object listenersLock = new Object(); // Lock object for changes to listeners
public void addLifecycleListener(LifecycleListener listener) {
synchronized (listenersLock) {
LifecycleListener results[] =
new LifecycleListener[listeners.length + 1];
for (int i = 0; i < listeners.length; i++)
results[i] = listeners[i];
results[listeners.length] = listener;
listeners = results;
}
}
public LifecycleListener[] findLifecycleListeners() {
return listeners;
}
public void fireLifecycleEvent(String type, Object data) {
LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
LifecycleListener interested[] = listeners;
for (int i = 0; i < interested.length; i++)
interested[i].lifecycleEvent(event);
}
public void removeLifecycleListener(LifecycleListener listener) {
synchronized (listenersLock) {
int n = -1;
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == listener) {
n = i;
break;
}
}
if (n < 0)
return;
LifecycleListener results[] =
new LifecycleListener[listeners.length - 1];
int j = 0;
for (int i = 0; i < listeners.length; i++) {
if (i != n)
results[j++] = listeners[i];
}
listeners = results;
}
}
}

 

模拟程序运行结果:

文章首发地址:Solinx

http://www.solinx.co/archives/86

 

Tomcat源码分析之—组件启动实现分析的更多相关文章

  1. Tomcat8源码笔记(七)组件启动Server Service Engine Host启动

    一.Tomcat启动的入口 Tomcat初始化简单流程前面博客介绍了一遍,组件除了StandardHost都有博客,欢迎大家指文中错误.Tomcat启动类是Bootstrap,而启动容器启动入口位于 ...

  2. 简单读!tomcat源码(一)启动与监听

    tomcat 作为知名的web容器,很棒! 本文简单了从其应用命令开始拆解,让我们对他有清晰的了解,揭开神秘的面纱!(冗长的代码流水线,给你一目了然) 话分两头: 1. tomcat是如何启动的? 2 ...

  3. Tomcat源码分析之—具体启动流程分析

    从Tomcat启动调用栈可知,Bootstrap类的main方法为整个Tomcat的入口,在init初始化Bootstrap类的时候为设置Catalina的工作路径也就是Catalina_HOME信息 ...

  4. Tomcat 源码分析(一)——启动与生命周期组件

    写在前面的话:读Tomcat源码也有段时间了,大领悟谈不上.一些小心得记录下来,供大家参考相护学习. 一.启动流程 Tomcat启动首先需要熟悉的是它的启动流程.和初学者第一天开始写Hello Wor ...

  5. Tomcat源码分析(从启动流程到请求处理)

    Tomcat 8.5下载地址 https://tomcat.apache.org/download-80.cgi Tomcat启动流程 Tomcat源码目录 catalina目录 catalina包含 ...

  6. Tomcat源码分析(下载、启动)

    1.下载Tomcat源代码: https://tomcat.apache.org/download-80.cgi 2. 解压以及创建必要目录和配置 解压.新建catalina-home目录,同时将目录 ...

  7. [Tomcat 源码分析系列] (二) : Tomcat 启动脚本-catalina.bat

    概述 Tomcat 的三个最重要的启动脚本: startup.bat catalina.bat setclasspath.bat 上一篇咱们分析了 startup.bat 脚本 这一篇咱们来分析 ca ...

  8. Tomcat源码分析——启动与停止服务

    前言 熟悉Tomcat的工程师们,肯定都知道Tomcat是如何启动与停止的.对于startup.sh.startup.bat.shutdown.sh.shutdown.bat等脚本或者批处理命令,大家 ...

  9. Tomcat源码分析三:Tomcat启动加载过程(一)的源码解析

    Tomcat启动加载过程(一)的源码解析 今天,我将分享用源码的方式讲解Tomcat启动的加载过程,关于Tomcat的架构请参阅<Tomcat源码分析二:先看看Tomcat的整体架构>一文 ...

随机推荐

  1. Oracle 数据库基础学习 (六) 子查询

    子查询在一个select中出现多个嵌套查询语句 1.在where子句中使用子查询(一般返回"单行单列" "单行多列" "多行单列"(可以提供 ...

  2. Verilog学习笔记简单功能实现(八)...............异步FIFO

    基本原理:       1.读写指针的工作原理 写指针:总是指向下一个将要被写入的单元,复位时,指向第1个单元(编号为0). 读指针:总是指向当前要被读出的数据,复位时,指向第1个单元(编号为0). ...

  3. WebDriver多线程并发

    要想多线程并发的运行WebDriver,必须同时满足2个条件,首先你的测试程序是多线程,其次需要用到Selenium Server.下载位置如下图: 下载下来后是一个jar包,需要在命令行中运行.里面 ...

  4. 本机jdbc连接报The user specified as a definer ('root'@'%') does not exist

    昨晚一台测试服务器连接本机的mysql时,有些调用存储过程报"The user specified as a definer ('root'@'%') does not exist" ...

  5. spring 整合 struts

    struts配置 objectFactory 在struts.xml添加,用spring工厂管理action对象 <constant name="struts.objectFactor ...

  6. scope='request'的bean预加载冲突

    Error creating bean with name 'authenticationSuccessServlet': Scope 'request' is not active for the ...

  7. web桌面程序之图标拖动排序的分析

    在web桌面程序里,图标拖动并重新排序是个比较常见的功能.这个功能我之前反复修改了好几遍,现在终于整理出了比较理想的解决思路,决定拿出来分享下. 这一功能主要有哪些难点呢?我总结了一下一共有2处难点: ...

  8. jquery+jplayer实现歌词同步的mp3音乐播放器效果

    实例预览 下载地址 实例代码 <div class="container"> <div class="demo"> <textar ...

  9. jQuery.swatches – 把 Div 变成可爱的调色板

    jQuery.swatches 是一款开源的 jQuery 插件,能够把一个 Div 转换成漂亮的调色板.您可以自定义你想要的类,使用不同的类可以生成不同的调色板.这个功能能够帮助设计师方便的挑选设计 ...

  10. 如何删掉Portal中的无效内容

    1.登陆到https://[PORTALURL]/arcgis/sharing/rest/ 2.使用内容的所有者或者门户管理员的账号登陆. 3.登陆后访问https://[PORTALURL]/arc ...