【Tomcat学习笔记】7-分析各个组件的init和start

栏目: 服务器 · 发布时间: 8年前

内容简介:【Tomcat学习笔记】7-分析各个组件的init和start

你TM写这种东西不烦吗?我也很无奈啊,太懒,只能靠写博客、吹牛逼驱动自己看代码啊。

下面分析每个组件时,只分析它初始化自己的那部分逻辑,其它各种如何调用子组件的逻辑就不一一赘述了。

StandardServer#initInternal

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);
                        }
                    } catch (URISyntaxException e) {
                        // Ignore
                    } catch (IOException e) {
                        // Ignore
                    }
                }
            }
        }
        cl = cl.getParent();
    }
}

从 Catalina 的 parentClassLoader 开始,向上一直遍历到 ExtClassLoader,把它们会加载的 jar 包都用ExtensionValidator记录下来,后面再 StandardContext 启动的时候,会用 ExtensionValidator 来校验 StandardContext 对应的 Web App 依赖的一些 jar 包是否已经被加进来了。

ContainerBase#initInternal

StandardEngine,StandardHost,StandardContext,StandardWrapper 四种容器本身的 initInternal 没有什么操作,主要都是调用ContainerBase#initInternal.

protected void initInternal()throwsLifecycleException{
    BlockingQueue<Runnable> startStopQueue = new LinkedBlockingQueue<>();
    startStopExecutor = new ThreadPoolExecutor(
            getStartStopThreadsInternal(),
            getStartStopThreadsInternal(), 10, TimeUnit.SECONDS,
            startStopQueue,
            new StartStopThreadFactory(getName() + "-startStop-"));
    startStopExecutor.allowCoreThreadTimeOut(true);
    super.initInternal();
}

这里主要就是创建一个线程池,用来处理子容器的 start 和 stop. 线程的数量 startStopThreads 默认是 1,如果配置的值小于等于0,则线程数为 Runtime.getRuntime().availableProcessors() + startStopThreads。如果部署了多个应用,配置多个线程可以并行部署,加快启动速度。

ContainerBase#startInternal

上一节 init 时候创建的线程池,在这里 start 的时候就派上用场了。将 child 的 start 任务提交到线程池里。

@Override
protected synchronized void startInternal()throwsLifecycleException{
    ...
    Container children[] = findChildren();
    List<Future<Void>> results = new ArrayList<>();
    for (int i = 0; i < children.length; i++) {
        results.add(startStopExecutor.submit(new StartChild(children[i])));
    }
    ...
}

但是这里有个问题,当 StandardHost 执行到这段代码时,它并没有 child, 这个时候 Context 并没有创建。 So, Context 以及 后面的 Wrapper 是如何创建和初始化的呢?谢晞鸣带着疑问继续翻着代码。。。。


以上所述就是小编给大家介绍的《【Tomcat学习笔记】7-分析各个组件的init和start》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深度学习入门

深度学习入门

[ 日] 斋藤康毅 / 陆宇杰 / 人民邮电出版社 / 2018-7 / 59.00元

本书是深度学习真正意义上的入门书,深入浅出地剖析了深度学习的原理和相关技术。书中使用Python3,尽量不依赖外部库或工具,从基本的数学知识出发,带领读者从零创建一个经典的深度学习网络,使读者在此过程中逐步理解深度学习。书中不仅介绍了深度学习和神经网络的概念、特征等基础知识,对误差反向传播法、卷积神经网络等也有深入讲解,此外还介绍了深度学习相关的实用技巧,自动驾驶、图像生成、强化学习等方面的应用,......一起来看看 《深度学习入门》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具