【问题标题】:What is the best way to iterate inside WSO2 EI?在 WSO2 EI 中迭代的最佳方法是什么?
【发布时间】:2020-01-23 17:42:49
【问题描述】:


在阅读了 WSO2 EI References 之后,我仍然对如何在 EI 序列中使用迭代器感到困惑。 就我而言,我有一个像这样的有效负载....

   {
...
   "array": [
    {"cpf": "12345678911"},
    {"cnpj":"12345678912346"}
   ]
}

所以我必须反复检查这些人是否存在使用其他网络服务。为了实现该流程,我使用迭代调解器来拆分消息,然后构建逻辑以进行如下验证..

实现此图像的代码如下:

 <iterate description="" expression="//jsonObject/array" id="myid">
                        <target>
                            <sequence>
                                <property expression="json-eval($.array.cpf)" name="tipoCPF" scope="default" type="STRING"/>
                                <filter description="" xpath="boolean(get-property('tipoCPF'))">
                                    <then>
                                        <property expression="json-eval($.array.cpf)" name="uri.var.cpf" scope="default" type="STRING"/>

                                        <call>
                                            <endpoint>
                                                <http method="get" uri-template="http://endpoint/service/{uri.var.cpf}"/>
                                            </endpoint>
                                        </call>
                                        <filter regex="200" source="get-property('axis2','HTTP_SC')">
                                            <then/>
                                            <else>
                                                <payloadFactory description="" media-type="json">
                                                    <format>{&#xd;
"code":"400",&#xd;
"error":"CPF inexistente"&#xd;
}</format>
                                                    <args/>
                                                </payloadFactory>
                                                <property name="HTTP_SC" scope="axis2" type="STRING" value="400"/>
                                                <respond/>
                                            </else>
                                        </filter>
                                    </then>
                                    <else>
                                        <property expression="json-eval($.array.cnpj)" name="tipoCNPJ" scope="default" type="STRING"/>
                                        <filter xpath="boolean(get-property('tipoCNPJ'))">
                                            <then>
                                                <property expression="json-eval($.array.cnpj)" name="uri.var.cnpj" scope="default" type="STRING"/>
                                                <header name="Authorization" scope="transport" value="Basic Y29yZS5jb25zdWx0aW5nOm8xNXRyZWI="/>
                                                <call>
                                                    <endpoint>
                                                        <http method="get" uri-template="http://endpoint/service/cnpj/{uri.var.cnpj}"/>
                                                    </endpoint>
                                                </call>
                                                <filter regex="200" source="get-property('axis2','HTTP_SC')">
                                                    <then/>
                                                    <else>
                                                        <payloadFactory media-type="json">
                                                            <format>{&#xd;
        "code":"400",&#xd;
        "error":"CNPJ inexistente"&#xd;
        }</format>
                                                            <args/>
                                                        </payloadFactory>
                                                        <property name="HTTP_SC" scope="axis2" type="STRING" value="400"/>
                                                        <respond/>
                                                    </else>
                                                </filter>
                                            </then>
                                            <else>
                                                <call>
                                                    <endpoint>
                                                        <http method="get" uri-template="http://endpoint/service/info"/>
                                                    </endpoint>
                                                </call>
                                            </else>
                                        </filter>
                                    </else>
                                </filter>
                            </sequence>
                        </target>
                    </iterate>

这个迭代器作为“序列”的一部分工作。 'Insequence' 旨在允许在数据库中插入新的合同信息。

问题:添加此迭代器后,服务开始在数据库中进行重复插入。看起来迭代没有在标签“迭代器”的边缘完成。这就像迭代继续到其余的序列。 尝试:为了解决这个问题,我尝试在迭代器之后添加一个聚合器中介器。但是或者没有任何效果结束重复的插入仍然存在,或者我收到一条错误消息。

那么在 WSO2 EI 中进行此迭代的正确乳清是什么?

【问题讨论】:

    标签: wso2 wso2esb wso2ei mediator ei


    【解决方案1】:

    正如您所提到的,迭代甚至会在迭代标记之外发生,直到使用聚合中介。要解决此问题,您需要在迭代调解器内部添加聚合调解器。这将停止迭代器标记本身内的迭代。

    对于您的用例,您可能需要在IterateMediator中设置continueParent="true",以便在iterate mediator之后继续进行中介,以便在数据库中进行插入操作。

    【讨论】:

      【解决方案2】:

      感谢阿鲁南的帮助!

      在您回答后,我尝试按如下方式添加聚合器

      聚合器的配置如下:

      ...
       <aggregate id="NIRO">
                                          <completeCondition>
                                              <messageCount max="-1" min="-1"/>
                                          </completeCondition>
                                          <onComplete expression="//jsonObject">
                                              <log description="" level="full" separator=";">
                                                  <property expression="json-eval($.)" name="jsonObject"/>
                                              </log>
                                          </onComplete>
                                      </aggregate>
                                  </sequence>
                              </target>
                          </iterate>
      

      你很伤心,我将迭代器属性“Continue Parent”更改为“true”。但是问题依然存在....

      【讨论】:

      • 您是否仍然收到重复条目?
      • 是的!问题依旧!
      • 您是否可以共享完整的突触配置(已删除敏感信息)?
      • 在哪里可以找到这个配置?
      • 您在上图中显示的中介流的源视图。
      【解决方案3】:

      正如另一个答案中所建议的,您需要

      • 使用 Aggregate 调解器来结束迭代
      • 当且仅当 Iterator 设置为 continueParent="true"

      但是,我不确定将它放入 &lt;iterate&gt; 是否有效。这是一个在Iterator之后使用Aggregate中介的可行解决方案。

      <sequence>
          <iterate continueParent="true" description="" expression="//jsonObject/array" id="myid">
              <target>
                  <your_sequence />
              </target>
          </iterate>
          <aggregate>
              <completeCondition>
                  <messageCount max="-1" min="-1" />
              </completeCondition>
              <onComplete enclosingElementProperty="//jsonObject/array" expression="/whatever/you/want"/>
          </aggregate>
      </sequence>
      

      注意您在 Iteration 中使用的 expression="//jsonObject/array",您需要在 AggregatorenclosingElementProperty 中使用它。这就是您的 EI 将如何知道它应该从哪个迭代器聚合(不是 100% 确定这一点,更多的是经验考虑)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        • 1970-01-01
        • 2016-05-05
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多