Multiple Backend Calls and Aggregated Responses with WSO2 EI

Thishani Lucas
Think Integration
Published in
3 min readApr 2, 2019

It is an integration scenario where a user wants to make multiple backend calls, get the response of each call and aggregate all the responses together. This blog post is about how we can achieve this requirement using the WSO2 EI.

To demonstrate this usecase, I’ll be using the echo service packed with the EI, as the backend. Also I’m using the EI 6.4.0 Let’s create a WSDL endpoint using the WSDL of the echo service.

<endpoint xmlns=”http://ws.apache.org/ns/synapse" name=”echoEndpoint”>
<wsdl uri=”http://localhost:8280/services/echo?wsdl" service=”echo” port=”echoHttpEndpoint”>
<suspendOnFailure>
<progressionFactor>1.0</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
<retryDelay>0</retryDelay>
</markForSuspension>
</wsdl>
</endpoint>

Let’s break the scenario in to two use cases. Assume we have the following payload.

<user>
<name>User01</name>
<number>45</number>
</user>

Now we want to call a backend with only the value of<name> and some other with only the value of<number>. To achieve this, we can use the EI’s clone mediator. Following is a sample API demonstrating this.

<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse"
name="testClone"
context="/testClone">
<resource methods="POST">
<inSequence>
<clone>
<target>
<sequence>
<payloadFactory media-type="xml">
<format>
<echo:echoString xmlns:echo="http://echo.services.core.carbon.wso2.org">
<in>$1</in>
</echo:echoString>
</format>
<args>
<arg evaluator="xml" expression="//name"/>
</args>
</payloadFactory>
<call>
<endpoint key="echoEndpoint"/>
</call>
</sequence>
</target>
<target>
<sequence>
<payloadFactory media-type="xml">
<format>
<echo:echoInt xmlns:echo="http://echo.services.core.carbon.wso2.org">
<in>$1</in>
</echo:echoInt>
</format>
<args>
<arg evaluator="xml" expression="//number"/>
</args>
</payloadFactory>
<call>
<endpoint key="echoEndpoint"/>
</call>
</sequence>
</target>
</clone>
<property name="results" scope="default">
<Results/>
</property>
<aggregate>
<completeCondition>
<messageCount min="-1" max="-1"/>
</completeCondition>
<onComplete expression="$body/*[1]" enclosingElementProperty="results">
<respond/>
</onComplete>
</aggregate>
</inSequence>
<faultSequence/>
</resource>
</api>

We call this API with the previous xml payload. In this API, we have a clone mediator with two targets. In the first target, we construct a payload with the value of <name> for the operation echoString. In the second target, we construct a payload for the operation echoInt with the value of <number>. In each target, we call the echoEndpoint and finally we have used an aggregate mediator to aggregate the responses of each call. Following will be the resultant payload. We have enclosed the responses of each call inside the <Results> tag.

<Results xmlns="http://ws.apache.org/ns/synapse">
<ns:echoStringResponse xmlns:ns="http://echo.services.core.carbon.wso2.org">
<return xmlns="">User01</return>
</ns:echoStringResponse>
<ns:echoIntResponse xmlns:ns="http://echo.services.core.carbon.wso2.org">
<return xmlns="">45</return>
</ns:echoIntResponse>
</Results>

Now assume we have the following payload.

<names>
<name>User01</name>
<name>User02</name>
</names>

We need to send each <name> separately to a backend and perform an operation. For this we’re going to use the iterate mediator.

<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse"
name="testIterate"
context="/testIterate">
<resource methods="POST">
<inSequence>
<iterate expression="//names/name">
<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="//name"/>
</args>
</payloadFactory>
<call>
<endpoint key="echoEndpoint"/>
</call>
</sequence>
</target>
</iterate>
<property name="results" scope="default">
<Results/>
</property>
<aggregate>
<completeCondition>
<messageCount min="-1" max="-1"/>
</completeCondition>
<onComplete expression="//return" enclosingElementProperty="results">
<respond/>
</onComplete>
</aggregate>
</inSequence>
</resource>
</api>

Here, inside the iterate mediator, we’re constructing a payload with the value of <name> tag. Then we’re calling the echoEndpoint. This is going to be iterated for each of the <name> tag. And finally we’re aggregating the responses of all the calls and enclosing it inside a <Results> tag. But this time, without capturing the entire response body, we’re retrieving only the <return> element. Following will be the result of this API.

<Results xmlns="http://ws.apache.org/ns/synapse">
<return xmlns="">User02</return>
<return xmlns="">User01</return>
</Results>

That’s all for this blog. Stay tuned for the next one!

Further readings:

--

--