前言
上篇文章講到了 Wrapper 的啓動。在這篇文章中初次提到了 Pipeline 和 Valve。Pipeline的實現類是 StandardPipeline,StandardPipeline 繼承自 LifecycleBase,而 Valve 的實現類則比較多,這些實現類都繼承自基類 ValveBase,而 ValveBase 繼承自 LifecycleMBeanBase。apache
1. Pipeline 與 Valve 的初始化
在 ContainerBase 中有一個 Pipeline 類型的對象,其聲明爲:segmentfault
/** * The Pipeline object with which this Container is associated. */ protected final Pipeline pipeline = new StandardPipeline(this);
public StandardPipeline(Container container) { super(); setContainer(container); } /** * The Container with which this Pipeline is associated. */ protected Container container = null; @Override public void setContainer(Container container) { this.container = container; }
能夠看出,每個 Container 對象都包含了一個 Pipeline 對象,包括 Engine、Host、Context、Wrapper。app
public StandardEngine() { super(); pipeline.setBasic(new StandardEngineValve()); /* Set the jmvRoute using the system property jvmRoute */ try { setJvmRoute(System.getProperty("jvmRoute")); } catch(Exception ex) { log.warn(sm.getString("standardEngine.jvmRouteFail")); } // By default, the engine will hold the reloading thread backgroundProcessorDelay = 10; }
public StandardHost() { super(); pipeline.setBasic(new StandardHostValve()); }
public StandardContext() { super(); pipeline.setBasic(new StandardContextValve()); broadcaster = new NotificationBroadcasterSupport(); // Set defaults if (!Globals.STRICT_SERVLET_COMPLIANCE) { // Strict servlet compliance requires all extension mapped servlets // to be checked against welcome files resourceOnlyServlets.add("jsp"); } }
public StandardWrapper() { super(); swValve=new StandardWrapperValve(); pipeline.setBasic(swValve); broadcaster = new NotificationBroadcasterSupport(); }
@Override public void setBasic(Valve valve) { // Change components if necessary Valve oldBasic = this.basic; if (oldBasic == valve) return; // Stop the old component if necessary if (oldBasic != null) { if (getState().isAvailable() && (oldBasic instanceof Lifecycle)) { try { ((Lifecycle) oldBasic).stop(); } catch (LifecycleException e) { log.error(sm.getString("standardPipeline.basic.stop"), e); } } if (oldBasic instanceof Contained) { try { ((Contained) oldBasic).setContainer(null); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); } } } // Start the new component if necessary if (valve == null) return; if (valve instanceof Contained) { ((Contained) valve).setContainer(this.container); } if (getState().isAvailable() && valve instanceof Lifecycle) { try { ((Lifecycle) valve).start(); } catch (LifecycleException e) { log.error(sm.getString("standardPipeline.basic.start"), e); return; } } // Update the pipeline Valve current = first; while (current != null) { if (current.getNext() == oldBasic) { current.setNext(valve); break; } current = current.getNext(); } this.basic = valve; }
在每一個容器的構造函數中,都調用了 Pipeline#setBasic 方法給各自的 Pipeline 都設置了一個 Valve 對象。
能夠看出,每一個不一樣的 Container 對應的 Valve 實現類是不一樣的。這些不一樣的 Valve 實現類的父類都是 ValveBase。jvm
public StandardEngineValve() { super(true); }
public StandardHostValve() { super(true); }
public StandardContextValve() { super(true); }
public StandardWrapperValve() { super(true); }
public ValveBase(boolean asyncSupported) { this.asyncSupported = asyncSupported; }
能夠看出在這四個類的構造方法中,都設置了 ValveBase 裏的 asyncSupported 屬性爲true。jsp
2. Pipeline#initInternal、startInternal 方法
在容器啓動的時候(ContainerBase#startInternal)調用了其成員屬性 Pipeline 的 start 方法。StandardPipeline,重寫了 LifecycleBase 的 initInternal 和 startInternal 方法async
@Override protected void initInternal() { // NOOP } /** * Start {@link Valve}s) in this pipeline and implement the requirements * of {@link LifecycleBase#startInternal()}. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ @Override protected synchronized void startInternal() throws LifecycleException { // Start the Valves in our pipeline (including the basic), if any Valve current = first; if (current == null) { current = basic; } while (current != null) { if (current instanceof Lifecycle) ((Lifecycle) current).start(); current = current.getNext(); } setState(LifecycleState.STARTING); }
StandardPipeline 的 initInternal 方法是一個空實現,而 startInternal 方法,則是依次調用本身關聯的 Valve 的 start 方法。ide
3. ValveBase#initInternal、startInternal 方法
Valve 的實現類 StandardEngineValve、StandardHostValve、StandardContextValve 都沒有重載 initInternal 和 startInternal 方法, StandardWrapperValve 也沒有重載 startInternal 方法,重載了 initInternal,可是 StandardWrapperValve#initInternal 方法是一個空方法。函數
@Override protected void initInternal() throws LifecycleException { super.initInternal(); containerLog = getContainer().getLogger(); } /** * Start this component and implement the requirements * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ @Override protected synchronized void startInternal() throws LifecycleException { setState(LifecycleState.STARTING); }
ValveBase#initInternal、startInternal 都很簡單。ui
小結
本文分析了 Pipeline 和 Valve 的初始化,以及 init 和 start 過程,這兩個組件的初始化、init、start 相對來講比較簡單。Pipeline 和 Valve 真正起做用的時候是在 Connector 使用容器 Container 處理請求的時候,Connector 會找到本身關聯的 Service 的裏的 Container 對象(也就是 Engine 對象),而後獲取這個對象的 Pipeline,經過這個 Pipeline 對象獲取 Pipeline 對象的 Valve 對象,最後經過調用 Valve 對象的 invoke 方法來處理請求,以下面的代碼this
connector.getService().getContainer().getPipeline() .getFirst().invoke(request, response);
固然,這些內容將在後面的文章中揭祕,本文在此很少作討論。關於 Valve#invoke 方法,每一個實現類的內容都不相同,這裏也很少作描述了。