【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

相关文章

Linux中的HTTPS协议原理分析

《Linux中的HTTPS协议原理分析》文章解释了HTTPS的必要性:HTTP明文传输易被篡改和劫持,HTTPS通过非对称加密协商对称密钥、CA证书认证和混合加密机制,有效防范中间人攻击,保障通信安全... 目录一、什么是加密和解密?二、为什么需要加密?三、常见的加密方式3.1 对称加密3.2非对称加密四、

MySQL中读写分离方案对比分析与选型建议

《MySQL中读写分离方案对比分析与选型建议》MySQL读写分离是提升数据库可用性和性能的常见手段,本文将围绕现实生产环境中常见的几种读写分离模式进行系统对比,希望对大家有所帮助... 目录一、问题背景介绍二、多种解决方案对比2.1 原生mysql主从复制2.2 Proxy层中间件:ProxySQL2.3

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1