This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Sample 751: Message Split Aggregate Using Templates

Objective: Demonstrate the use of tempaltes in Split/Aggregate scenario.

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

    <proxy name="SplitAggregateProxy">
        <target>
            <inSequence>
		<!--use iterate sequence template to split incoming request and send-->
                <call-template target="iter_func">
                        <with-param xmlns:m0="http://services.samples" name="iter_expr" value="{{//m0:getQuote/m0:request}}"/>
 			<with-param xmlns:m0="http://services.samples" name="attach_path" value="{{//m0:getQuote}}"/>
                </call-template>
            </inSequence>
            <outSequence>
		<!--use aggregate sequence template to combine the responses and send back-->
                <call-template target="aggr_func">
                        <with-param xmlns:m0="http://services.samples" name="aggr_expr" value="{{//m0:getQuoteResponse}}"/>
                </call-template>
            </outSequence>
        </target>
    </proxy>

    <!--this sequence template will aggregate the responses , merge and send back to the client. This takes aggregate
	expression as an argument-->
    <template xmlns="http://ws.apache.org/ns/synapse" name="aggr_func">
        <parameter name="aggr_expr"/>
        <sequence>
            <log level="full"/>
            <aggregate>
                <completeCondition>
                    <messageCount min="-1" max="-1"/>
                </completeCondition>
                <onComplete expression="$func:aggr_expr">
                    <log level="full" />
                    <send/>
                </onComplete>
            </aggregate>
        </sequence>
    </template>

    <!--this sequence template will iterate through stock quote symbols ,split and send them to endpoints. Takes Iterate
	expression and soap attaach path as arguments -->
    <template xmlns="http://ws.apache.org/ns/synapse" name="iter_func">
        <parameter name="iter_expr"/>
        <parameter name="attach_path"/>
        <sequence>

            <iterate  xmlns:m0="http://services.samples" preservePayload="true" attachPath="$func:attach_path" expression="$func:iter_expr">
                <target>
                    <sequence>
                        <send>
                            <endpoint>
                                <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                            </endpoint>
                        </send>
                    </sequence>
                </target>
            </iterate>
        </sequence>
    </template>
</definitions>

Prerequisites:

  • Deploy the SimpleStockQuoteService in sample Axis2 server and start it on port 9000.
  • Start ESB with the sample configuration 751 (i.e. wso2esb-samples -sn 751).

This will be the same sample demonstrated in advanced mediation with Iterate mediator used to split the messages in to parts and process them asynchronously and then aggregate the responses coming in to ESB. Only addition will be two sequence templates 'iter_func' and 'aggr_func' which will split the message and aggregate the responses back , respectively.

Invoke the client as follows.

ant stockquote -Daddurl=http://localhost:8280/services/SplitAggregateProxy -Ditr=4

Sequence Template 'iter_func' takes iterate expressions and attach path as arguments. Sequence Template 'aggr_func' takes aggregate expression as argument.

In this sample we are using special dynamic xpath expressions(in double curly braces {{expr}}) in call-template mediator so that they are evaluated only when iteration and aggregation is done. Note that function scope (ie:-$func) is used to access template parameters in 'iter_func' and 'aggr_func'.