内容简介:【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学习笔记】组件声明周期
- 【Tomcat学习笔记】组件声明周期
- ReactNative学习笔记(四)之自定义文本组件与Image组件
- Vue笔记(六)——Vue组件通信&Vuex
- ReactNative学习笔记(七)之重新组织应用的组件
- vue移动端引用第三方组件-lytab(学习笔记)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。