tomcat8 源码分析 | 组件及启动过程 (2)

Digester是一种将xml转化为java对象的事件驱动型工具,通过读取xml文件,当识别到特定的节点的时候会执行特定的动作,创建java对象或者执行对象的某个方法

在这里插入图片描述

通过Digester去创建了Catania中的大量初始化工作,具体详见源码:

// 创建server实例 digester.addObjectCreate("Server", "org.apache.catalina.core.StandardServer", "className"); //创建Executor digester.addObjectCreate("Server/Service/Executor", "org.apache.catalina.core.StandardThreadExecutor", "className"); ...等等大量初始化工作...

接着讲,getServer().init()方法的作用是初始化Sever,调用LifecycleBase的init()方法,在init方法中调用的是StandardServer类initInternal()方法

StandardServer

StandardServer类图如下:

jiegou

StandardServer类initInternal()方法:

/** * Invoke a pre-startup initialization. This is used to allow connectors * to bind to restricted ports under Unix operating environments. */ @Override protected void initInternal() throws LifecycleException { super.initInternal(); ...省略很多,但是主要的在下面... // Initialize our defined Services for (int i = 0; i < services.length; i++) { //调用services的init services[i].init(); } }

前面的时候讲过一个server初始化多个services;

StandardService

services[i].init();初始化的是StandardService类,类图如下

在这里插入图片描述

StandardService的initInternal() 方法的工作是初始化engine组件,初始化线程池,初始化mapperListener,初始化connector

/** * Invoke a pre-startup initialization. This is used to allow connectors * to bind to restricted ports under Unix operating environments. */ @Override protected void initInternal() throws LifecycleException { super.initInternal(); //初始化engine engine.init(); //初始化线程池 // Initialize any Executors for (Executor executor : findExecutors()) { if (executor instanceof JmxEnabled) { ((JmxEnabled) executor).setDomain(getDomain()); } executor.init(); } //初始化mapperListener // Initialize mapper listener mapperListener.init(); //初始化connector connector.init(); }

初始化executor,mapperListener,connector后面再讲其作用,先接初始化engine

StandardEngine

StandardEngine的类图如下:

在这里插入图片描述

在StandardEngine的初始化中并没有直接调用host的初始化,而是调用的父类containerBase的initInternal的方法:

//StandardEngine @Override protected void initInternal() throws LifecycleException { // Ensure that a Realm is present before any attempt is made to start // one. This will create the default NullRealm if necessary. getRealm(); super.initInternal(); } //containerBase @Override protected void initInternal() throws LifecycleException { BlockingQueue<Runnable> startStopQueue = new LinkedBlockingQueue<>(); startStopExecutor = new ThreadPoolExecutor( getStartStopThreadsInternal(), getStartStopThreadsInternal(), 10, TimeUnit.SECONDS, startStopQueue, new StartStopThreadFactory(getName() + "-startStop-")); startStopExecutor.allowCoreThreadTimeOut(true); super.initInternal(); }

host的init是在start阶段去做的,所以后面再说

executor

executor.init();默认调用LifecycleMBeanBase的initInternal方法

mapperListener

mapperListener.init();也默认调用LifecycleMBeanBase的initInternal方法

connector

connector的初始化调用Connector类的initInternal方法,主要是new了一个CoyoteAdapter,初始化protocolHandler

@Override protected void initInternal() throws LifecycleException { super.initInternal(); // 实例化 CoyoteAdapter 适配器 adapter = new CoyoteAdapter(this); protocolHandler.setAdapter(adapter); ...... try { //初始化 protocolHandler protocolHandler.init(); } catch (Exception e) { throw new LifecycleException( sm.getString("coyoteConnector.protocolHandlerInitializationFailed"), e); } }

ProtocolHandler.init();的实现:

AbstractProtocol是调用endpoint的init方法,这个方法中调用bind()

@Override public void init() throws Exception { //初始化endpoint endpoint.init(); }

bind()针对不同的io类型提供了三种的默认实现

在这里插入图片描述

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxyfj.html