2017-04-20 42 views
0

我很新WSO2 ESB我对如何在我的ESB项目中实现“if(){...} else {...}”类似结构存在以下疑问。我用什么表达式来执行与WSO2 ESB过滤器介体中属性值相关的选择?

在其中,我的工作我有这个 物业中介接着是 日志中介的应用程序,只需打印该属性的值的输入流

所以,这样的事情:

<property expression="count(//ds:Sample)" name="total_samples" scope="default" type="STRING" xmlns:ds="http://ws.wso2.org/dataservice"/> 

<log level="custom"> 
    <property expression="$ctx:total_samples" name="total samples: "/> 
</log> 

这工作正常。

This total_samples property包含从以前的DSS服务调用中获得的记录数(我没有放在代码中)。

所以这total_samples的价值属性可以是:

  • :如果通过DSS服务实现的查询返回0的记录。
  • 数值> 0:如果此查询返回了一些记录。

现在我需要在这个时候做的仅仅是链上的“如果(){...}否则{...}”结构不同的打印日志消息如果total_samples财产值为或其他数字> 0

这应该是一个版本简单的任务,但我对如何实现它的一些疑惑:

FIRST的疑问:展望上的联机文档,在我看来存在2调解员,可用于执行选择在WSB流程中:开关介体和过滤器介体。他们在我看来非常相似。这些调解员有什么区别?我的目的是什么更好?

SECOND的疑问:在我看来,这些调解员只能在XPATH表达(类似计数(// DS:样品)),他们可以直接在我的财产的工作(类似“ $ ctx:total_samples“)?

第三的疑问:在这个阶段,我在流动中实现这样的事情:

<property expression="count(//ds:Sample)" name="total_samples" scope="default" type="STRING" xmlns:ds="http://ws.wso2.org/dataservice"/> 

<log level="custom"> 
    <property expression="$ctx:total_samples" name="total samples: "/> 
</log> 

<filter xpath="EXPRESSION THAT DO SOMETHING LIKE: $ctx:total_samples == 0"> 
    <then> 
     <log description="No Resource Log"> 
      <property name="message" value="&quot;EMPTY RESULTSET, NO RESOURCES TO PROCESS&quot;"/> 
     </log> 
    </then> 
    <else> 
     <log description="Found Resource Log"> 
      <property name="message" value="&quot;Resources have been found, will be processed&quot;"/> 
     </log> 
    </else> 
</filter> 

好了,所以我的问题是:什么是我所为表达式中使用的情况下,如果进入$ ctx:total_samples value is in the following line?

<filter xpath="EXPRESSION THAT DO SOMETHING LIKE: $ctx:total_samples == 0"> 

回答

2

一个更通用的解决方案:

<?xml version="1.0" encoding="UTF-8"?> 
<proxy xmlns="http://ws.apache.org/ns/synapse" 
     name="testIfElse" 
     transports="https http" 
     startOnLoad="true"> 
    <target> 
     <inSequence> 
      <payloadFactory media-type="xml"> 
       <format> 
       <ds:Sample xmlns:ds="http://ws.wso2.org/dataservice"> 
        <ds:INT_ID>1</ds:INT_ID> 
        <ds:INT_ID>2</ds:INT_ID> 
        <ds:INT_ID>3</ds:INT_ID> 
       </ds:Sample> 
       </format> 
       <args> 
       </args> 
      </payloadFactory>  
     <property expression="count(//ds:Sample/ds:INT_ID)" name="total_samples" scope="default" xmlns:ds="http://ws.wso2.org/dataservice" type="DOUBLE"/> 
     <property value="0" name="initial_value" scope="default" type="DOUBLE"/> 
     <property expression="fn:number($ctx:total_samples) &gt; fn:number($ctx:initial_value)" name="result" scope="default"/> 

     <log level="custom"> 
      <property expression="$ctx:initial_value" name="initial value: "/> 
      <property expression="fn:number($ctx:total_samples)" name="total samples: "/> 
      <property expression="$ctx:result" name="if total samples greater than initial value: "/> 
     </log> 

     <filter xpath="$ctx:result" regex="true"> 
      <then> 
       <log description="Found Resource Log"> 
        <property name="message" value="&quot;Resources have been found, will be processed&quot;"/> 
       </log> 

      </then> 
      <else> 
       <log description="No Resource Log"> 
        <property name="message" value="&quot;EMPTY RESULTSET, NO RESOURCES TO PROCESS&quot;"/> 
       </log> 
      </else> 
     </filter> 
     </inSequence> 
     <outSequence> 
      <log level="full"/> 
     <drop/> 
     </outSequence> 
     <faultSequence/> 
    </target> 
</proxy> 
2

用这句话

<filter xpath="fn:number(get-property('total_samples')) = fn:number(0)"> 
0

你实际上是在问三个问题在这里,所以我会尽量回答所有的人:

  1. Switch调解器允许多例如,你可能会遇到count = 0,count = 1和count> 1的情况。另一方面,过滤器中介就像经典的if/else。如果这比做x,否则做。

  2. 过滤器介体可以使用'source'和'regex'属性比较某些值与正则表达式,在这种情况下它会检查它们是否匹配。或者它使用xpath属性,在这种情况下,它会将xpath表达式的结果评估为布尔值。在xpath表达式以及源代码中,您可以使用$ ctx直接引用您的属性。例如:

  3. 你可以做的是

    <filter xpath="fn:number($ctx:total_samples) = fn:number(0)">