深入分析理解Tomcat体系结构

  由上图可知Tomcat的顶层容器是Server,而且一个Tomcat对应一个Server,一个server有多个service提供服务.service包含两个重要组件:Connector和Container.这个后面详细讲解.这个Server由谁来管理呢?当然是Catalina了,她是tomcat的管理类,她的三个方法load,start,stop分别用来管理整个服务器的生命周期.

Load方法:Load方法根据conf/server.xml文件创建Server并调用Server的init方法进行初始化.Server的init方法调用所有service的init方法,service的init方法调用所有Connector和Container的init方法.整个初始化工作就完成了.

Start方法:用于启动服务,类似init,也是逐层进行启动.

Stop方法:用于关闭服务,类似init,也是逐层调用关闭.

最后,CatAlina中的await方法非常重要,这个方法调用Server中的await方法,这个方法的作用就是进入一个循环,保持主线程不退出.

Tomcat组件启动过程

深入分析理解Tomcat体系结构

Bootstrap的启动过程

Tomcat启动的入口方法就是Bootstrap中的main方法,代码如下:

 public static void main(String args[]) {
        if (daemon == null) {
            // Don't set daemon until init() has completed
            Bootstrap bootstrap = new Bootstrap();
            try {//初始化ClassLoader,创建了Catalina实例,赋值给catalinaDaemon
                bootstrap.init();
            } catch (Throwable t) {
                handleThrowable(t);
                t.printStackTrace();
                return;
            }
            daemon = bootstrap;
        } else {
            // When running as a service the call to stop will be on a new
            // thread so make sure the correct class loader is used to prevent
            // a range of class not found exceptions.
            Thread.currentThread().setContextClassLoader(daemon.catalinaLoader);
        }
        //根据args参数执行不同的命令
        try {
            String command = "start";//默认执行start
            if (args.length > 0) {
                command = args[args.length - 1];
            }
            if (command.equals("startd")) {
                args[args.length - 1] = "start";
                daemon.load(args);
                daemon.start();
            } else if (command.equals("stopd")) {
                args[args.length - 1] = "stop";
                daemon.stop();
            } else if (command.equals("start")) {
                daemon.setAwait(true);
                daemon.load(args);
                daemon.start();
            } else if (command.equals("stop")) {
                daemon.stopServer(args);
            } else if (command.equals("configtest")) {
                daemon.load(args);
                if (null==daemon.getServer()) {
                    System.exit(1);
                }
                System.exit(0);
            } else {
                log.warn("Bootstrap: command \"" + command + "\" does not exist.");
            }
        } catch (Throwable t) {
            // Unwrap the Exception for clearer error reporting
            if (t instanceof InvocationTargetException &&
                    t.getCause() != null) {
                t = t.getCause();
            }
            handleThrowable(t);
            t.printStackTrace();
            System.exit(1);
        }
}
View Code

相关文章:

  • 2021-10-07
  • 2021-10-15
  • 2021-10-15
  • 2021-08-07
  • 2019-12-28
  • 2021-11-24
  • 2021-12-23
猜你喜欢
  • 2021-11-05
  • 2021-11-14
  • 2021-08-30
  • 2021-10-15
  • 2021-11-28
  • 2022-01-01
  • 2021-12-02
  • 2021-11-16
相关资源
相似解决方案