`
goodscript
  • 浏览: 71943 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

tomcat bootstrap启动步骤

 
阅读更多
TOMCAT以一个责任链贯穿Server的启动过程。首先是读取配置文件、由Server启动一个service,由service把connector和container组装起来对外界提供服务。
在tomcat6中connector包括三种不同的connector:
1、Http Connector 基于HTTP协议,负责建立HTTP连接。它又分为BIO Http Connector与NIO Http Connector两种,后者提供非阻塞IO与长连接Comet支持。
2、AJP Connector, 基于AJP协议,AJP是专门设计用来为tomcat与http服务器之间通信专门定制的协议,能提供较高的通信速度和效率。如与Apache服务器集成时,采用这个协议。
3、APR HTTP Connector, 用C实现,通过JNI调用的。主要提升对静态资源(如HTML、图片、CSS、JS等)的访问性能。现在这个库已独立出来可用在任何项目中。Tomcat在配置APR之后性能非常强劲

Container 是容器的父接口,所有子容器都必须实现这个接口,Container 容器的设计最能体现责任链的设计模式;它有四个子容器组件构成,分别是:Engine、Host、Context、Wrapper。这四个组件不是平行的,而是父子关系,  Engine 包含 Host,Host 包含 Context,Context 包含 Wrapper,
通常一个 Servlet class 对应一个 Wrapper,如果有多个 Servlet 就可以定义多个 Wrapper


在启动的过程中service责任链的下一个节点是Engine,再下一个节点是Host、然后context。
等所有的容器都初始化完成了,tomcat也就启动完成


然而贯穿整个启动过程的责任链由一个简单的接口来实现:lifecycle
lifecycle实际上就是观察者模式中的被观察者 规范了生命周期组件的状态和操作方法
lifecycleListener 监听接口 实际上就是观察者模式中的观察者  监听在生命周期组件的各种状态变化
LifecycleEvent 事件定义接口 可以通过构造函数构造生命周期中的任何一个状态



1、bootstrap.main 

启动的入口
2、catalina.init
加载类库 初始化守护线程
 if (daemon == null) {
            daemon = new Bootstrap();
            try {
                daemon.init();
            } catch (Throwable t) {
                t.printStackTrace();
                return;
            }
        }

3、catalina.load  
catalina就是tomcat的守护线程  load方法加载server.xml配置文档  加载责任链相关的监听信息
                daemon.setAwait(true);
                daemon.load(args);
                daemon.start();

4、catalina.start 
启动StandardServer.start方法
// Start the new server
        if (getServer() instanceof Lifecycle) {
            try {
                ((Lifecycle) getServer()).start();
            } catch (LifecycleException e) {
                log.error("Catalina.start: ", e);
            }
        }

5、StandardServer.start
从这里开始 启动责任链 启动的之前先触发before_start动作、然后触发start动作、接着启动下级容器standardService、然后触发after_start动作
// 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);

6、standardService.start
先触发before_start动作、然后触发start动作、接着启动下级容器StandardEngine、启动连接器[Connector[HTTP/1.1-8080], Connector[AJP/1.3-8009]]、然后触发after_start动作
// Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
        if(log.isInfoEnabled())
            log.info(sm.getString("standardService.start.name", this.name));
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;

        // Start our defined Container first
        if (container != null) {
            synchronized (container) {
                if (container instanceof Lifecycle) {
                    ((Lifecycle) container).start();
                }
            }
        }

        synchronized (executors) {
            for ( int i=0; i<executors.size(); i++ ) {
                executors.get(i).start();
            }
        }

        // Start our defined Connectors second
        synchronized (connectors) {
            for (int i = 0; i < connectors.length; i++) {
                try {
                    ((Lifecycle) connectors[i]).start();
                } catch (Exception e) {
                    log.error(sm.getString(
                            "standardService.connector.startFailed",
                            connectors[i]), e);
                }
            }
        }
        
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);

7、standardEngine.start
补充说明一下standardEngine、StandardHost、StandardContext都继承了ContainerBase类  ContainerBase类中重写了start方法  standardEngine只要调用super.start() 。容器ContainerBase会递归启动standardEngine下的所有子容器
  // Standard container startup
        super.start();


8、containerBase.start
containerBase.start方法里面除了启动下级子容器外还做了不少动作 例如日志组件、集群功能加载、启动容器后台守护线程

        // Validate and update our current component state
        if (started) {
            if(log.isInfoEnabled())
                log.info(sm.getString("containerBase.alreadyStarted", logName()));
            return;
        }
        
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);

        started = true;

        // Start our subordinate components, if any
        if ((loader != null) && (loader instanceof Lifecycle))
            ((Lifecycle) loader).start();
        logger = null;
        getLogger();
        if ((logger != null) && (logger instanceof Lifecycle))
            ((Lifecycle) logger).start();
        if ((manager != null) && (manager instanceof Lifecycle))
            ((Lifecycle) manager).start();
        if ((cluster != null) && (cluster instanceof Lifecycle))
            ((Lifecycle) cluster).start();
        if ((realm != null) && (realm instanceof Lifecycle))
            ((Lifecycle) realm).start();
        if ((resources != null) && (resources instanceof Lifecycle))
            ((Lifecycle) resources).start();

        // Start our child containers, if any
        Container children[] = findChildren();
        for (int i = 0; i < children.length; i++) {
            if (children[i] instanceof Lifecycle)
                ((Lifecycle) children[i]).start();
        }

        // Start the Valves in our pipeline (including the basic), if any
        if (pipeline instanceof Lifecycle)
            ((Lifecycle) pipeline).start();

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(START_EVENT, null);

        // Start our thread
        threadStart();

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);

    

StandardHost.start
StandardContext.start
分享到:
评论

相关推荐

    JAVA物流运输管理系统.zip

    启动步骤 1.新建mysql数据库名称为tms,并导入项目sql语句 (表比较多可能时间较长) 2.将项目导入idea开发工具中(maven下载依赖需要等待) 3.修改项目数据连接地址TMS\src\main\resources\db.properties文件4-6行 ...

    JSP Servlet 初学者教程 - 25 个步骤

    Step01.md:在 Tomcat 中启动并运行 Web 应用程序 Step02.md:第一个JSP Step03.md : 添加 GET 参数名称 Step04.md : 添加另一个获取参数密码 Step05.md:让我们添加一个表单 Step06.md:新建表单和doPost Step07.md...

    基于springboot+bootstrap+thymeleaf的物联网一站式宠物管理平台(领养、救助、商城)设计.zip

    本系统应有Tomcat(网页应用服务器), MySQL(数据库服务), Redis(NoSQL数据库提供缓存服务), RabbitMQ(消息队列服务), ElasticSearch(检索服务), FastDFS(分布式文件存储系统)等组件, 因此有以下安装步骤: ...

    design-shibboleth-idp-theme:Shibboleth IdP> 3.2的Bootstrap Italia模板

    设计Shibboleth-IDP主题 Shibboleth IdP&gt; 3.2的Bootstrap Italia模板 ... 该项目通过Designers Italia 定义的模式和组件继承了定义的所有功能,组件,网格和实用... 重新启动Web服务器(tomcat,jetty或其他servlet容器)

    毕业设计-SSM毕设管理系统

    BSManager--基于SSM的毕设管理系统 介绍 基于SSM框架的毕设管理系统,后端采用Spring+SpringMVC+Mybatis,前端采用bootstrap,实现了教师端、学生端、管理员端的后台运行。 步骤 拉取代码 ...启动tomcat

    使用springboot+mybatis+mysql实现的毕业设计-旅游网站.zip

    毕业设计项目,将ssm技术切换为springboot,使用内置的tomcat即可运行项目。 管理系统 url: http://localhost:8080/managerLoginPage user: admin password: 123 用户门户网站 url: http://localhost:8080/user ...

    activiti入门学习

    acticiti、XSS、bootstrap、单点登录 运行步骤:1....运行db_schame.sql文件 4.Tomcat启动项目 5.访问:localhost:8080/simple_activiti/to_login.do 注:单点登录、XSS都是有用的,自己可以测试一下

    毕设新项目-基于pringboot实现的旅游网站源码+sql数据库+项目部署说明.zip

    将ssm技术切换为springboot,使用内置的tomcat即可运行项目。 管理系统 - url: http://localhost:8080/managerLoginPage - user: admin password: 123 用户门户网站 - url: http://localhost:8080/user - user: ...

    SpringBoot开发非常美观的java博客系统(包含后台管理功能)

    准备 IDE (如果你不看源码,可以忽略下面的步骤,直接通过Maven编译war包:mvn clean package -DskipTests) IDE 需要配置的东西 编码方式设为UTF-8 配置Maven 设置Jdk8 关于这些配置,网上有一大把的资料,所以此处...

    jspBB 论坛(问答)系统 v1.0

    Ant Design Pro 5jspBB 论坛(问答)系统搭建步骤Maven3.3 或更高版本。并配置阿里云Maven仓库镜像。 IntelliJ IDEA 2018.3 或更高版本。需Kotlin1.3支持。 通过pom.xml导入项目。等待jar包下载完成。 在MySQL中...

    JAVA核心知识点整理(有效)

    1. 目录 1. 2. 目录 .........................................................................................................................................................1 JVM ........................

Global site tag (gtag.js) - Google Analytics