Dropping a message within an iterate or a for-each mediator -WSO2 EI

Lashan Sivaganeshan
2 min readApr 22, 2018

--

Hi Folks,

Consider a situation which requires to iterate through a message using an iterate mediator [1] or a for-each mediator [2]. Both of the above mediators have their use cases and as explained in the following documents [1] and [2], based on the requirement you can choose either one of them to get the job done.

However due to the parallel nature of the above mediators the respond mediator [3] which is supposed to stop the processing on the current message to send the message back to the client, does not work as in normal scenarios (without any iterations) when it’s hit. Instead the processing is continued until the end of the iterations. This post explains a mechanism which can be used to stop the processing of the message flow based on a condition within an iteration using the drop mediator [4]. The condition to check when the above drop mediator should be executed is incorporated using a filter mediator [5].

The following example assumes that an error sequence or a template is invoked inside the related sequence which contains the iterate/for-each mediator, on an error as shown below using a for-each mediator.

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="ReconIteratorSeq" onError="ReconJsonErrorSeq">

<foreach xmlns:ns="http://org.apache.synapse/xsd" id="Loop" expression="//transactions">
<sequence>
...
...
</sequence>
</foreach>
<filter xmlns:ns="http://org.apache.synapse/xsd" source="get-property('ErrorComparator')" regex="true">
<then>
<log>
<property name="Validation Failure" value="True"/>
</log>
<drop/>
</then>
<else>
<log>
<property name="Validation Failure" value="False"/>
</log>
</else>
</filter>
</sequence>

Please note the filter mediator above which checks the value of a property named ‘ErrorComparator’. If the value is ‘true’, the drop mediator will be invoked and the message will be dropped terminating the message to be processed further. In the above example the ‘ErrorComparator’ property is set to true inside ReconJsonErrorSeq sequence as follows because it’s invoked when there’s an error in the current sequence.

<property name="ErrorComparator" value="true" scope="default" type="BOOLEAN"/>

Hope the above mechanism provides a workaround to the deviation of the respond mediator within iterations.

Thanks.

[1]. https://docs.wso2.com/display/EI611/Iterate+Mediator

[2]. https://docs.wso2.com/display/EI611/Iterate+Mediator

[3]. https://docs.wso2.com/display/EI611/Respond+Mediator

[4]. https://docs.wso2.com/display/EI611/Drop+Mediator

[5]. https://docs.wso2.com/display/EI611/Filter+Mediator

--

--

Lashan Sivaganeshan

What you search is out there. It's a matter of pressing the right keys.