Over a million developers have joined DZone.

Nested Iterator with WSO2 ESB

Build APIs from SQL and NoSQL data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

Please find the following sample which demonstrates the Nested iterator.

The following is the source of the configuration.

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <registry provider="org.wso2.carbon.mediation.registry.WSO2Registry">
        <parameter name="cachableDuration">15000</parameter>
    </registry>
    <sequence name="IterateMediatorSequence">
        <iterate xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" xmlns:m0="http://services.samples" id="iterate1" preservePayload="true" attachPath="//m0:getQuotes" expression="//m0:getQuotes/m0:getQuote">
            <target>
                <sequence>
                    <log level="full">
                        <property name="target1" value="************After 1st Iterate before 2nd Iterate*************"/>
                    </log>
                    <iterate id="iterate2" preservePayload="true" attachPath="//m0:getQuote" expression="//m0:getQuotes/m0:getQuote/m0:request">
                        <target>
                            <sequence>
                                <log level="full">
                                    <property name="target2333" value="************After 2nd Iterate and before payload factory*************"/>
                                </log>
                                <payloadFactory>
                                    <format>
                                        <m0:getQuote>
                                            <m0:request>
                                                <m0:symbol>$1</m0:symbol>
                                            </m0:request>
                                        </m0:getQuote>
                                    </format>
                                    <args>
                                        <arg expression="//m0:getQuote/m0:request/m0:symbol"/>
                                    </args>
                                </payloadFactory>
                                <log level="full">
                                    <property name="target1" value="************after payload factory*************"/>
                                </log>
                                <send>
                                    <endpoint>
                                        <address uri="http://localhost:9001/services/SimpleStockQuoteService"/>
                                    </endpoint>
                                </send>
                            </sequence>
                        </target>
                    </iterate>
                </sequence>
            </target>
        </iterate>
    </sequence>
    <sequence name="aggregateMessages">
        <aggregate id="iterate2">
            <completeCondition>
                <messageCount min="-1" max="-1"/>
            </completeCondition>
            <onComplete xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" xmlns:m0="http://services.samples" expression="//m0:getQuoteResponse">
                <log level="full"/>
                <aggregate id="iterate1">
                    <completeCondition>
                        <messageCount min="-1" max="-1"/>
                    </completeCondition>
                    <onComplete expression="//m0:getQuoteResponse">
                        <send/>
                    </onComplete>
                </aggregate>
            </onComplete>
        </aggregate>
    </sequence>
    <sequence name="fault">
        <log level="full">
            <property name="MESSAGE" value="Executing default 'fault' sequence"/>
            <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
            <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
        </log>
        <drop/>
    </sequence>
    <sequence name="main">
        <in>
            <sequence key="IterateMediatorSequence"/>
        </in>
        <out>
            <sequence key="aggregateMessages"/>
        </out>
    </sequence>
</definitions> 

Use the following request with soapui in order to test the above sample.

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
 <m0:getQuotes xmlns:m0="http://services.samples">
<m0:getQuote>
<m0:request>
<m0:symbol>SUN9</m0:symbol>
</m0:request>
<m0:request>
<m0:symbol>SUN10</m0:symbol>
</m0:request>
</m0:getQuote>
</m0:getQuotes>
</soapenv:Body>
</soapenv:Envelope>
From the second iterate mediator, messages retrieved from the first iterator are again iterated into smaller messages as below

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<m0:getQuote xmlns:m0="http://services.samples">
<m0:request>
<m0:symbol>SUN10</m0:symbol>
</m0:request>
</m0:getQuote>
</soapenv:Body>
</soapenv:Envelope> 

The Integration Zone is brought to you in partnership with CA Technologies.  Create app backends instantly with REST APIs and reactive logic using CA Live API Creator.

Topics:

Published at DZone with permission of Achala Chathuranga Aponso , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}