diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/ForEachMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/ForEachMediatorFactory.java index 1fbdf1d83e..ee91de8795 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/ForEachMediatorFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/ForEachMediatorFactory.java @@ -54,6 +54,9 @@ public class ForEachMediatorFactory extends AbstractMediatorFactory { private static final QName ID_Q = new QName(XMLConfigConstants.NULL_NAMESPACE, "id"); + private static final QName CONTINUE_IN_FAULT_Q + = new QName(XMLConfigConstants.NULL_NAMESPACE, "continueLoopOnFailure"); + public QName getTagQName() { return FOREACH_Q; } @@ -69,6 +72,11 @@ protected Mediator createSpecificMediator(OMElement elem, mediator.setId(id.getAttributeValue()); } + OMAttribute continueOnFail = elem.getAttribute(CONTINUE_IN_FAULT_Q); + if (continueOnFail != null) { + mediator.setContinueLoopOnFailure(Boolean.parseBoolean(continueOnFail.getAttributeValue())); + } + OMAttribute expression = elem.getAttribute(ATT_EXPRN); if (expression != null) { try { diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java index 6593945de6..eafddca78d 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java @@ -65,6 +65,8 @@ public class ForEachMediator extends AbstractMediator implements ManagedLifecycl private String id; + private boolean continueLoopOnFailure = false; + private static final String FOREACH_ORIGINAL_MESSAGE = "FOREACH_ORIGINAL_MESSAGE"; private static final String FOREACH_COUNTER = "FOREACH_COUNTER"; @@ -137,7 +139,23 @@ public boolean mediate(MessageContext synCtx) { for (JsonElement element : iterableJsonArray) { try { updateIteratedMessage(synCtx, element); - boolean mediateResult = mediateSequence(synCtx); + boolean mediateResult; + if (continueLoopOnFailure) { + try { + mediateResult = mediateSequence(synCtx); + } catch (Exception ex) { + if (log.isDebugEnabled()) { + log.warn("Error occurred while mediating the sequence for the foreach mediator, " + + "Continuing with the remaining.", ex); + } else { + log.warn("Error occurred while mediating the sequence for the foreach mediator, " + + ex.getMessage() + ". Continuing with the remaining."); + } + continue; + } + } else { + mediateResult = mediateSequence(synCtx); + } modifiedPayloadArray.add(EIPUtils.tryParseJsonString(parser, JsonUtil.jsonPayloadToString(((Axis2MessageContext) synCtx).getAxis2MessageContext()))); msgCounter++; @@ -146,10 +164,19 @@ public boolean mediate(MessageContext synCtx) { + msgCounter); } synCtx.setProperty(counterPropName, msgCounter); - if (!mediateResult) { // break the loop if mediate result is false + if (!mediateResult && !continueLoopOnFailure) {// break the loop if mediate result is false break; } } catch (AxisFault axisFault) { + if (continueLoopOnFailure) { + if (log.isDebugEnabled()) { + log.warn("Error occurred in the foreach mediator, Continuing with the remaining.", axisFault); + } else { + log.warn("Error occurred in the foreach mediator, " + axisFault.getMessage() + + ". Continuing with the remaining."); + } + continue; + } handleException("Error updating the stream with iterater element : " + element.toString(), axisFault, synCtx); } @@ -211,7 +238,22 @@ public boolean mediate(MessageContext synCtx) { } catch (AxisFault axisFault) { handleException("Error creating an iterated copy of the message", axisFault, synCtx); } - boolean mediateResult = mediateSequence(iteratedMsgCtx); + boolean mediateResult = false; + if (continueLoopOnFailure) { + try { + mediateResult = mediateSequence(iteratedMsgCtx); + } catch (Exception ex) { + if (log.isDebugEnabled()) { + log.warn("Error occurred while mediating the sequence for the foreach mediator, " + + "Continuing with the remaining.", ex); + } else { + log.warn("Error occurred while mediating the sequence for the foreach mediator, " + + ex.getMessage() + ". Continuing with the remaining."); + } + } + } else { + mediateResult = mediateSequence(iteratedMsgCtx); + } //add the mediated element to the parent from original message context parent.addChild(iteratedMsgCtx.getEnvelope().getBody().getFirstElement()); @@ -222,7 +264,7 @@ public boolean mediate(MessageContext synCtx) { } synCtx.setProperty(counterPropName, msgCounter); - if (!mediateResult) { // break the loop if mediate result is false + if (!mediateResult && !continueLoopOnFailure) { // break the loop if mediate result is false break; } } @@ -464,4 +506,8 @@ public String getId() { public void setId(String id) { this.id = id; } + + public void setContinueLoopOnFailure(boolean continueOnFail) { + this.continueLoopOnFailure = continueOnFail; + } }