【Tomcat9源码分析】Container、Pipeline和Vavle设计

2023-11-21 16:40

本文主要是介绍【Tomcat9源码分析】Container、Pipeline和Vavle设计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载请注明出处:http://blog.csdn.net/linxdcn/article/details/73800100


1 概述

如果你对Tomcat的整个框架、组件、请求流程不熟悉,建议你先阅读以下3篇Tomcat概述性的文章,再来看本篇文章:

【Tomcat9源码分析】组件与框架概述
【Tomcat9源码分析】生命周期、启动、停止概述
【Tomcat9源码分析】请求过程概述

Container是Tomcat中很重要的容器,主要包含Engine、Host、Context和Wrapper,其采用了责任链的设计模式,来处理一次请求。


2 Container分析

Container是一个接口,Tomcat提供了ContainerBase作为其实现的基类。

2.1 字段
public abstract class ContainerBase implements Container {// 子容器protected final HashMap<String, Container> children = new HashMap<>();// 监听事件protected final List<ContainerListener> listeners = new CopyOnWriteArrayList<>();// Container对应的Pipelineprotected final Pipeline pipeline = new StandardPipeline(this);// 领域对象private volatile Realm realm = null;
}
2.2 启动
public abstract class ContainerBase implements Container {@Overrideprotected synchronized void startInternal() throws LifecycleException {// 1 启动领域对象Realm realm = getRealmInternal();if (realm instanceof Lifecycle) {((Lifecycle) realm).start();}// 2 启动子容器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])));}// 3 启动Pipelineif (pipeline instanceof Lifecycle)((Lifecycle) pipeline).start();// 4 设置状态,启动本线程setState(LifecycleState.STARTING);threadStart();}
}
  1. 启动领域对象
  2. 启动子容器
  3. 启动Pipeline
  4. 设置状态,启动本线程

3 Pipeline分析

Pipeline是一个接口,其实现类是StandardPipeline

public class StandardPipeline extends LifecycleBase implements Pipeline {// 基础阀门protected Valve basic = null;// 关联的容器protected Container container = null;// 第一个阀门protected Valve first = null;@Overrideprotected synchronized void startInternal() throws LifecycleException {Valve current = first;if (current == null) {current = basic;}// 依次启动所有Value,Value是一个链表结构while (current != null) {if (current instanceof Lifecycle)((Lifecycle) current).start();current = current.getNext();}setState(LifecycleState.STARTING);}
}

4 Valve分析

Valve是一个接口,其基本实现的BaseValve类。

public abstract class ValveBase extends LifecycleMBeanBase implements Contained, Valve {// 关联的Containerprotected Container container = null;// 下一个Valveprotected Valve next = null;@Overridepublic Container getContainer() {return container;}// 初始化@Overrideprotected void initInternal() throws LifecycleException {super.initInternal();containerLog = getContainer().getLogger();}// 启动@Overrideprotected synchronized void startInternal() throws LifecycleException {setState(LifecycleState.STARTING);}
}

5 几个重要的Valve

5.1 StandardEngineValve
final class StandardEngineValve extends ValveBase {public final void invoke(Request request, Response response)throws IOException, ServletException {// 1 由request获取hostHost host = request.getHost();if (host == null) {// ...省略return;}// 2 调用host的pipeline的vavlehost.getPipeline().getFirst().invoke(request, response);}
}
  1. 根据request定位到可以处理的host对象
  2. 依次调用host里的pipeline上的valve
5.2 StandardEngineValve
final class StandardHostValve extends ValveBase {public final void invoke(Request request, Response response)throws IOException, ServletException {// 1 由request获取contextContext context = request.getContext();try {// 2 调用context里的pipeline上的valvecontext.getPipeline().getFirst().invoke(request, response);// response已经返回response.setSuspended(false);Throwable t = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);// 3 如果有错误,重定向到错误页if (response.isErrorReportRequired()) {if (t != null) {throwable(request, response, t);} else {status(request, response);}}} }
}
  1. 由request获取context
  2. 调用context里的pipeline上的valve
  3. 如果有错误,重定向到错误页
5.3 StandardContextValve
final class StandardContextValve extends ValveBase {@Overridepublic final void invoke(Request request, Response response)throws IOException, ServletException {// 由request获取wrapperWrapper wrapper = request.getWrapper();// ...省略// 调用wrapper里的pipeline上的valvewrapper.getPipeline().getFirst().invoke(request, response);}
}
  1. 由request获取wrapper
  2. 调用wrapper里的pipeline上的valve
5.4 StandardWrapperValve
final class StandardWrapperValve extends ValveBase {public final void invoke(Request request, Response response)throws IOException, ServletException {// 1 获取wrapper, contextStandardWrapper wrapper = (StandardWrapper) getContainer();Servlet servlet = null;Context context = (Context) wrapper.getParent();// 2 加载servlettry {if (!unavailable) {servlet = wrapper.allocate();}} // ...省略catch// 3 新建一个 filter 链表ApplicationFilterChain filterChain =ApplicationFilterFactory.createFilterChain(request, wrapper, servlet);// servlet会放在 filter 链表最后,并且最后会调用servlet的service方法try {if ((servlet != null) && (filterChain != null)) {// Swallow output if neededif (context.getSwallowOutput()) {// 4 调用 filter 链表上的 doFilterfilterChain.doFilter(request.getRequest(),response.getResponse());}} } // ...省略catch}
}
  1. 获取wrapper, context
  2. 加载servlet
  3. 新建一个 filter 链表,servlet会放在 filter 链表最后,并且最后会调用servlet的service方法
  4. 调用 filter 链表上的 doFilter

5 总结




转载请注明出处:http://blog.csdn.net/linxdcn/article/details/73800100

这篇关于【Tomcat9源码分析】Container、Pipeline和Vavle设计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/403632

相关文章

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o