1


After reading of WSO2 EI References, I still confuse about how use iterators inside a EI sequence. In my case I have a payload like this....

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

So I have to iterate to check if those guys exist using another web services. in order to achieve that flow, I am using the iterate mediator to split the message and then building the logic to make those validations as follows..

enter image description here

The code that implements this image is following:

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

This iterator work as part inside of the an 'insequence'. The 'Insequence' is deigned to allow to insert new contracts information inside the Database.

Problem: after add this iterator, the service starts to make duplicated insertions inside Database. It´s looks like the iteration don´t finish in the edge of tags 'iterator'. It´s like the iteration continues to the rest of insequence. Try: In order to solve this problem i try to add an aggregator mediator after the iterator. But or doesn't have any effect end the duplicated insert persist, or I receive an error message.

So What is the correct whey to make this iterations inside WSO2 EI?

Community
  • 1
  • 1
ChelloFera
  • 349
  • 1
  • 3
  • 16

3 Answers3

0

As you have mentioned, Iteration will occur even outside the iterate tag, until the Aggregate mediator is used. To resolve this issue, you need to add the aggregate mediator inside the iterate mediator. This will stop the iteration within the iterator tag itself.

For your use case, you may need to set continueParent="true" in the IterateMediator, so that the mediation will continue after the iterate mediator for the insertion Operation in database.

Arunan Sugunakumar
  • 3,311
  • 3
  • 12
  • 20
0

Thanks for the helping Arunan!

After your answer I try to add the Aggregator as follows
Adding the Aggregator Mediator

The config of aggregator is following:

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

As you sad I change the iterator property 'Continue Parent' to 'true'. But the problem persists....

ChelloFera
  • 349
  • 1
  • 3
  • 16
0

As proposed in the other answer, you need to

  • use an Aggregate mediator to close the iteration
  • If and only if the Iterator is set to continueParent="true"

However, I am not sure that putting it inside the <iterate> works. Here is a working solution using an Aggregate mediator right after your Iterator.

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

Notice expression="//jsonObject/array" you used in your Iteration, you'll need to use it in the Aggregator's enclosingElementProperty. This is how your EI will know from which iterator it should aggregate from (not 100% sure about this point, more of an empirical consideration).

zar3bski
  • 2,773
  • 7
  • 25
  • 58