Monday, January 17, 2022

Iterator and aggregator mediator example using call mediator with blocking='true'

This example showcase how to use a call mediator with blocking=true (Synchronous way - wait for the API response and call next) inside the iterator mediator and aggregate response inside the aggregator mediator.

Sample Request payload used:

<Bank>

<Account>21424124214</Account>

<Account>35353553343</Account>

<Account>74564352352</Account>

<Account>86643526546</Account>

<Account>35465626565</Account>

<Account>25454625466</Account>

<Account>23564342424</Account>

<Account>23453654665</Account>

</Bank>

Sample code:

<?xml version="1.0" encoding="UTF-8"?>

<proxy xmlns="http://ws.apache.org/ns/synapse"

       name="test"

       transports="http https"

       startOnLoad="true">

   <description/>

   <target>

      <inSequence>

         <property name="countRequests"

                   expression="count(//Account)"

                   scope="default"

                   type="STRING"

                   description="countRequests"/>

         <iterate id="IterateAccountCreate" expression="//Account" sequential="true">

            <target>

               <sequence>

                  <payloadFactory media-type="xml">

                     <format>

                        <echo:echoString xmlns:echo="http://echo.services.core.carbon.wso2.org">

                           <in xmlns="">$1</in>

                        </echo:echoString>

                     </format>

                     <args>

                        <arg evaluator="xml" expression="//Account/text()"/>

                     </args>

                  </payloadFactory>

                  <property name="TRANSPORT_HEADERS"

                            scope="axis2"

                            action="remove"

                            description="TRANSPORT_HEADERS"/>

                  <property name="OperationName"

                            value="echoString"

                            scope="default"

                            type="STRING"

                            description="OperationName"/>

                  <header name="To" scope="default" action="remove"/>

                  <header name="Action" scope="default" value="urn:echoString"/>

                  <header name="SOAPAction" scope="transport" value="urn:echoString"/>

                  <log category="DEBUG" description="***Request***">

                     <property name="request" expression="$body"/>

                  </log>

                  <call blocking="true">

                     <endpoint>

                        <http method="POST" uri-template="http://prabod:8313/services/echo"/>

                     </endpoint>

                  </call>

                  <log category="DEBUG"

                       separator="***Response***"

                       description="***Response***">

                     <property name="create_instanties_response" expression="$body"/>

                  </log>

  <loopback/>

               </sequence>

            </target>

         </iterate>

      </inSequence>

      <outSequence>

<aggregate description="Aggregate response" id="IterateAccountCreate">

<completeCondition timeout="10">

</completeCondition>

<onComplete expression="//ns:echoStringResponse"

xmlns:ns="http://echo.services.core.carbon.wso2.org">

<log description="***aggregated***" separator="***aggregated***">

<property expression="$body" name="aggregated_body" />

</log>

<respond/>

</onComplete>

</aggregate>

      </outSequence>

   </target>

</proxy>

In this example, I'm calling the mock backend to test the scenario.
http://prabod:8313/services/echo

This will iterate all the account numbers and call the backend by passing the account number inside the iterator mediator. Finally, aggregate all the responses into one response payload inside aggregator mediator. In this scenario, I need this to be synchronous. In that case, we need to use the call mediator with blocking="true" and the aggregator mediator needs to be inside out-sequence.