Webx框架:Valve詳解

Valve用於控制請求的執行流程。它採用責任鏈的設計模式(類似於struts的攔截器)。valve的意思是閥,閥控制水流(網絡請求)的走向。


自定義閥。

public class MyValve implements Valve {
    public void invoke(PipelineContext pipelineContext) throws Exception {
        System.out.println("valve started.");
        pipelineContext.invokeNext(); // 調用後序 valves
        System.out.println("valve ended.");
    }
}

<services:pipeline xmlns="http://www.alibaba.com/schema/services/pipeline/valves">
...
<valve class="com.alibaba.myapp.pipeline.MyValve" />
...
</services:pipeline>


調用一個閥。可以通過Autowired獲取一個閥。

@Autowired
private Pipeline myPipeline;
public void invokePipeline() {
    PipelineInvocationHandle invocation = myPipeline.newInvocation();
    invocation.invoke();
}


一個valve可以調用另外一個Pipeline。可以中斷任意一個pipeline調用。

pipelineContext.breakPipeline(0); // level=0,中斷當前 pipeline
pipelineContext.breakPipeline(1); // level=1,中斷上一級 pipeline
pipelineContext.breakPipeline("label"); // 中斷到指定 label 的上級 pipeline
// 以上調用相當於:
pipelineContext.breakPipeline(pipelineContext.findLabel("label"));
pipelineContext.breakPipeline(Pipeline.TOP_LABEL); // 終止所有 pipelines


內置valve。


loop。

<loop loopCounterName="count" maxLoopCount="10">
<valve />
<break-if test="..." />
</loop>

<while maxLoopCount="10">
<conditions:condition class="..." />
<valve />
</while>

<if>
<conditions:condition class="..." />
<valve />
</if>

<choose>
<when test="1 == 2">
<valve />
</when>
<when>
<conditions:condition class="..." />
<valve />
</when>
<otherwise>
<valve />
</otherwise>
</choose>

在循環中使用
<exit />

在循環中使用
<break-if test="count &gt; 2" />
<break-if toLabel="MY_LOOP">
<conditions:condition class="..." />
</break-if>
<break-unless test="count &lt;= 2" />

<try-catch-finally>
<try>
<valve />
</try>
<catch exceptionName="myexception">
<valve />
</catch>
<finally>
<valve />
</finally>
</try-catch-finally>

條件支持jexl和自定義的condition類。
<if>
<conditions:jexl-condition expr="loopCount == 2" />
<break />
</if>

等價:
<if test="loopCount == 2">
<break />
</if>

<all-of>
<condition1 />
<condition2 />
<condition3 />
</all-of>

<any-of>
<condition1 />
<condition2 />
<condition3 />
</any-of>

<none-of>
<condition1 />
<condition2 />
<condition3 />
</none-of>


下面介紹一下常見的閥門。


analyzeURL。分析URL,作用是得到target。它有一個homepage參數,作用是默認頁面。


choose。相當於java中的switch語句。條件可以通過兩種屬性指定,target-extension-condition或者target-condition。下面是choose語句的例子。

<choose>
  <when>
    <pl-conditions:target-extension-condition extension="null, vm, jsp" />
    ...
  </when>
  <when>
    <pl-conditions:target-extension-condition extension="do" />
    ...
  </when>
  <otherwise>
    ...
  </otherwise>
</choose>


performAction。執行Action來處理表單。webx中的Action和其他框架中的不一樣,這裏只處理表單。其他的框架中可能還會在Action中渲染頁面。


performTemplateScreen。它的作用是查找對應的Screen代碼,並執行。如果target爲xxx/yyy/zzz那麼框架會依次尋找screen.xxx.yyy.Zzz、screen.xxx.yyy.Default、screen.xxx.Default、screen.Default,並調用它的execute(Context,HttpServletRequest)方法。在哪裏尋找這些類呢?可以在webx.xml中的module-loader/search-packages中指定根包。


renderTemplate。渲染模板。如果target是xxx/yyy/zzz那麼框架會依次嘗試尋找/templates/screen/xxx/yyy/zzz、/templates/screen/xxx/yyy/default、/templates/screen/xxx/default、/templates/screen/default。如果沒有找到模板,會報404的錯誤。找到screen模板以後,框架還會嘗試尋找layout:/templates/layout/xxx/yyy/zzz、/templates/layout/xxx/yyy/default等。


breakUnlessTargetRedirected。跳出循環的條件。如果有內部重定向發生,那麼跳出循環。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章