Enterprise Integration Zone is brought to you in partnership with:

I am a graduate of University of Westminster UK (IIT) B Sc Software Engineering ( First Class Honours). I am a Sun Certified Java Programmer for Java 2 platform, 1.5 versions (SCJP 1.5). – 93% and a Sun Certified Web Component Developer 97%. I work as a Software Engineer at WSO2. Achala is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Nested Iterator with WSO2 ESB

02.23.2013
| 1964 views |
  • submit to reddit
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> 

Published at DZone with permission of Achala Chathuranga Aponso, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)