【Tomcat9源码分析】Container、Pipeline和Vavle设计
来源:互联网 发布:tensorflow车牌识别 编辑:程序博客网 时间:2024/06/08 12:22
转载请注明出处: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对应的Pipeline protected final Pipeline pipeline = new StandardPipeline(this); // 领域对象 private volatile Realm realm = null;}
2.2 启动
public abstract class ContainerBase implements Container { @Override protected 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 启动Pipeline if (pipeline instanceof Lifecycle) ((Lifecycle) pipeline).start(); // 4 设置状态,启动本线程 setState(LifecycleState.STARTING); threadStart(); }}
- 启动领域对象
- 启动子容器
- 启动Pipeline
- 设置状态,启动本线程
3 Pipeline分析
Pipeline
是一个接口,其实现类是StandardPipeline
。
public class StandardPipeline extends LifecycleBase implements Pipeline { // 基础阀门 protected Valve basic = null; // 关联的容器 protected Container container = null; // 第一个阀门 protected Valve first = null; @Override protected 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 { // 关联的Container protected Container container = null; // 下一个Valve protected Valve next = null; @Override public Container getContainer() { return container; } // 初始化 @Override protected void initInternal() throws LifecycleException { super.initInternal(); containerLog = getContainer().getLogger(); } // 启动 @Override protected 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获取host Host host = request.getHost(); if (host == null) { // ...省略 return; } // 2 调用host的pipeline的vavle host.getPipeline().getFirst().invoke(request, response); }}
- 根据request定位到可以处理的host对象
- 依次调用host里的pipeline上的valve
5.2 StandardEngineValve
final class StandardHostValve extends ValveBase { public final void invoke(Request request, Response response) throws IOException, ServletException { // 1 由request获取context Context context = request.getContext(); try { // 2 调用context里的pipeline上的valve context.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); } } } }}
- 由request获取context
- 调用context里的pipeline上的valve
- 如果有错误,重定向到错误页
5.3 StandardContextValve
final class StandardContextValve extends ValveBase { @Override public final void invoke(Request request, Response response) throws IOException, ServletException { // 由request获取wrapper Wrapper wrapper = request.getWrapper(); // ...省略 // 调用wrapper里的pipeline上的valve wrapper.getPipeline().getFirst().invoke(request, response); }}
- 由request获取wrapper
- 调用wrapper里的pipeline上的valve
5.4 StandardWrapperValve
final class StandardWrapperValve extends ValveBase { public final void invoke(Request request, Response response) throws IOException, ServletException { // 1 获取wrapper, context StandardWrapper wrapper = (StandardWrapper) getContainer(); Servlet servlet = null; Context context = (Context) wrapper.getParent(); // 2 加载servlet try { 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 needed if (context.getSwallowOutput()) { // 4 调用 filter 链表上的 doFilter filterChain.doFilter(request.getRequest(), response.getResponse()); } } } // ...省略catch }}
- 获取wrapper, context
- 加载servlet
- 新建一个 filter 链表,servlet会放在 filter 链表最后,并且最后会调用servlet的service方法
- 调用 filter 链表上的 doFilter
5 总结
转载请注明出处:http://blog.csdn.net/linxdcn/article/details/73800100
阅读全文
0 0
- 【Tomcat9源码分析】Container、Pipeline和Vavle设计
- Tomcat源码阅读之Container与Pipeline的设计
- 【Tomcat9源码分析】Mapper路由映射器的设计
- 【Tomcat9源码分析】组件与框架概述
- 【Tomcat9源码分析】生命周期、启动、停止概述
- 【Tomcat9源码分析】请求过程概述
- 【Tomcat9源码分析】NIO连接器实现
- Tomcat9源代码分析(一)-源码切入点
- 【Tomcat9源码分析】源码下载、编译与调试
- docker container DNS配置介绍和源码分析
- docker container DNS配置介绍和源码分析
- netty4源码分析——pipeline关联
- Swift源码分析----swift-container-auditor
- Swift源码分析----swift-container-info
- Swift源码分析----swift-container-replicator
- Swift源码分析----swift-container-updater
- 《看透springMVC源码》之Container分析
- 【containerd 1.0 源码分析】ctr run container 源码分析
- call与apply
- PHP面向对象三大特性之封装基础
- Java后端WebSocket的Tomcat实现(转载)
- Git基本操作
- CSS布局 ——display,position, float
- 【Tomcat9源码分析】Container、Pipeline和Vavle设计
- 在Linux下编译Google leveldb数据库及在C++中操作示例 标签: linux数据库googlec++makefile浏览器 2012-03-14 09:15 955人阅读 评论(0)
- 【Nginx】——反向代理
- Linux中vim操作指令
- MyBatis动态SQL
- 【Linux】下的线程详解
- Android基础控件——EditText的自定义、高仿iOS的UITextField
- 芒果iOS开发之NSComparisonResult比较结果
- 消息机制之文件下载与断点续传,多线程下载