How tomcat works 读书笔记十七 启动tomcat 上

一路跋山涉水,这是最后一章了。

关于tomcat的启动,有两个类,一个是Catalina类,一个是Bootstrap类。
理论上,两个类可以和到一起,但是为了支持多种运行模式,又把他们分开了。
为了让用户更方便的启动tomcat,还有批处理文件startup.bat(终于见到最顶层的模块了!!)

Catalina

先说Catalina
它里面有一个Server组件(不懂的看第14章)。同时在它的start方法中包含一个Digester对象(不懂的看第15章)用来解析conf/下的Server.xml(这个xml装配起Catalina类里面的那个Server组件)。
同时Catalina本身也有main方法,在运行main方法的时候需要加上启动参数。可选start,stop,debug等等
我们看看process方法
    public void process(String args[]) {

        setCatalinaHome();     //设定Home与Base的值为user.dir
        setCatalinaBase();
      
    if (arguments(args))   //省略try catch
             execute();        //检查args的格式       
    }
    protected void execute() throws Exception {
        if (starting)
            start();
        else if (stopping)
            stop();
    }
    我们主要看如何启动。
   
  protected void start() { //省略了大量非核心代码 及try catch
    ...
        // Create and execute our Digester
        Digester digester = createStartDigester();  //这个要是不懂看15章
        File file = configFile();                   //指定文件为conf/server.xml
            InputSource is =new InputSource("file://" + file.getAbsolutePath());
            FileInputStream fis = new FileInputStream(file);
            is.setByteStream(fis);
            digester.push(this);        //把catalina自己放了进去  
                    //catalina里有server组件
            digester.parse(is);
            fis.close();
      
    ...
        // Start the new server
        if (server instanceof Lifecycle) {
                server.initialize();
                // Wait for the server to be told to shut down
                server.await();  //上面的英文再给大家解释一下吧
                         //除非收到SHUTDOWM的指令 就一直阻塞在这里
                 //这要是不懂 回去看第二三章
        }
    ...
        // Shut down the server
        if (server instanceof Lifecycle) {
                ((Lifecycle) server).stop();
        }
    }

这里有一个问题
server.initialize();
server从哪里来的?
别急看createStartDigester方法。
 
 protected Digester createStartDigester() {

        // Initialize the digester
        Digester digester = new Digester();
        if (debug)
            digester.setDebug(999);
        digester.setValidating(false);

        // Configure the actions we will be using
        digester.addObjectCreate("Server",
                                 "org.apache.catalina.core.StandardServer",
                                 "className");
        digester.addSetProperties("Server");
    //解释一下下面
    //在解析server.xml时 如果碰到Server这个模式
    //就调用栈底元素的方法名为setServer参数为org.apache.catalina.Server的方法
    //把Server自己(就是上面addObjectCreate产生的那个)注入栈底的那个元素
    //那么栈底是什么呢?
    //digester.push(this); 看看这行代码还有Catalian的setServer方法
        digester.addSetNext("Server",
                            "setServer",
                            "org.apache.catalina.Server");
    
    .....
    return digester
}


Bootstrap类

现在说说Bootstrap
Bootstrap的main方法会实例化一个Catalina对象,并调用它的process方法。
在main方法中,首先会创建三个类加载器。
为什么?就是为了不让应用程序中的类使用WEB-INF/Class和WEB-INF/lib之外的类。
具体怎么做的看第八章。
  public static void main(String args[]) {// 省略非核心代码及try catch
        // Construct the class loaders we will need
        ClassLoader commonLoader = null;
        ClassLoader catalinaLoader = null;
        ClassLoader sharedLoader = null;
      ...

            // Load our startup class and call its process() method
            // Instantiate a startup class instance
            if (debug >= 1)
                log("Loading startup class");
            Class startupClass =
                catalinaLoader.loadClass
                ("org.apache.catalina.startup.Catalina");
            Object startupInstance = startupClass.newInstance(); //加载Catalina类

            // Call the process() method
            if (debug >= 1)
                log("Calling startup class process() method");
            methodName = "process";
            paramTypes = new Class[1];
            paramTypes[0] = args.getClass();
            paramValues = new Object[1];
            paramValues[0] = args;
            method =
                startupInstance.getClass().getMethod(methodName, paramTypes);
            method.invoke(startupInstance, paramValues);  //调用Catalina的process方法
    }


关于startup.bat的知识,我们下一节再谈... 哎还有一节呀


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值