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。在对循环条件。假设有内部发生重定向,所以圈外。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4621219.html