我必须使用什么表达式来执行与 WSO2 ESB 过滤器调解器中的 属性 值相关的选择?

What expression have I to use to perform a choice related to a property value in a WSO2 ESB filter mediator?

我是 WSO2 ESB 的新手,我对如何实现 "if(){...} else{...}" 类结构有以下疑问在我的 ESB 项目中。

所以在我正在处理的应用程序的输入流中,我有这个 属性 中介,后跟一个 log 中介 简单地打印这个 属性 的值,像这样:

<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>

这很好用。

这个total_samples 属性包含了上一次调用DSS服务得到的记录数(我没有放在代码里) .

所以这个 total_samples 属性 的值可能是:

现在我需要做的只是链接一个 n "if(){...} else{...}" 结构,如果 total_samples 属性 值为 0 或任何数字 >0.

这应该是一个非常简单的任务,但我对如何实现它有一些疑问:

第一个疑问: 查看在线文档在我看来似乎存在 2 个可用于在 WSB 流程中执行选择的中介:开关 调解器和 filter 调解器。在我看来,它们非常相似。这些调解员之间有什么区别?什么对我的目的更好?

第二个疑问: 在我看来,这些中介仅适用于 XPATH 表达式(类似于 count( //ds:Sample)), 他们可以直接在我的 属性 上工作吗(类似于 "$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[=,我必须使用什么作为表达式进入 案例62=] 值是 0 在下一行?

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

使用这个表达式

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

更通用的解决方案:

<?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>

你在这里真的问了三个问题,所以我会尽力回答所有问题:

  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)">