浅析Tomcat之Pipeline和Value

来源:互联网 发布:东南大学软件测试专业 编辑:程序博客网 时间:2024/06/09 21:09
浅析Tomcat之Pipeline和Value

Tomcat的容器中,内容的执行是通过一个管道来控制的,它定义了一个Pipeline,4个层次的容器都持有了一个Pipeline用以执行预定义好的任务.而具体的任务则装载在Value中,也就是所谓的阀.

我们知道在连接适配器中的服务方法通过了调用connector.getService().getContainer().getPipeline().getFirst().invoke(request, response)的调用将请求转移到容器中执行.它实际就是调用了容器的Pipeline中的任务,即依次执行预定义好的任务(Value).Pipeline的实现是一个链表结构.我们先看看它其中的节点实现.

public abstract class ValveBase extends LifecycleMBeanBase    implements Contained, Valve{    /**     * The next Valve in the pipeline this Valve is a component of.     */    protected Valve next = null;    @Override    public Container getContainer() {        return (container);    }    @Override    public void setContainer(Container container) {        this.container = container;    }    @Override    public Valve getNext(){        return (next);    }    @Override    public void setNext(Valve valve){        this.next = valve;    }    @Override    public abstract void invoke(Request request, Response response)        throws IOException, ServletException;    @Override    public void event(Request request, Response response, CometEvent event)        throws IOException, ServletException {        // Perform the request        getNext().event(request, response, event);    }}

 上述精简过的代码是实现了Value接口的基础类ValueBase,它有一个Value类型的内部属性next,即同一个Pipeline中的后续Value的引用.如果玩过Java数据结构或作STL的基本上不难理解这是一个单向链表的节点.继承这个ValueBase针对不同的容器实现了不同版本的阀如StandardEngineValue,StandardHostValue,StandardContextValue,StandardWrapperValve等.他们之间不同的实现就是invoke和event的方法不同.而实际上也就是请求的路由选择,Filter应用和Servlet的处理(此部分内容后续博文解释).

Pipeline的标准实现是StandardPipeline.它的类注释是Standard implementation of a processing Pipeline that will invoke a series of Valves that have been configured to be called in order.This implementation can be used for any type of Container.意思是它用于依次执行已经配置好的阀.

0 0