Dropping a message within an iterate or a for-each mediator -WSO2 EI
Consider a situation which requires to iterate through a message using an iterate mediator  or a for-each mediator . Both of the above mediators have their use cases and as explained in the following documents  and , 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  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 . The condition to check when the above drop mediator should be executed is incorporated using a filter mediator .
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">
</foreach> <filter xmlns:ns="http://org.apache.synapse/xsd" source="get-property('ErrorComparator')" regex="true">
<property name="Validation Failure" value="True"/>
<property name="Validation Failure" value="False"/>
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.