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

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

内容简介:【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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

数据驱动设计

数据驱动设计

[美]罗谢尔·肯(RochelleKing)、[美]伊丽莎白F.邱吉尔(Elizabeth F Churchill)、Caitlin Tan / 傅婕 / 机械工业出版社 / 2018-8 / 69.00元

本书旨在帮你了解数据引导设计的基本原则,了解数据与设计流程整合的价值,避免常见的陷阱与误区。本书重点关注定量实验与A/B测试,因为我们发现,数据分析与设计实践在此鲜有交集,但相对的潜在价值与机会缺大。本书提供了一些关于在组织中开展数据实践的观点。通过阅读这本书,你将转变你的团队的工作方式,从数据中获得大收益。后希望你可以在衡量指标的选择、佳展示方式与展示时机、测试以及设计意图增强方面,自信地表达自......一起来看看 《数据驱动设计》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具