Skip to content

Commit

Permalink
Add option to continue iterations on failure for foreach mediator
Browse files Browse the repository at this point in the history
Add option to continue iterations on failure for foreach mediator
  • Loading branch information
GDLMadushanka committed May 21, 2024
1 parent 0b973e5 commit 4fdedae
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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++;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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());

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -464,4 +506,8 @@ public String getId() {
public void setId(String id) {
this.id = id;
}

public void setContinueLoopOnFailure(boolean continueOnFail) {
this.continueLoopOnFailure = continueOnFail;
}
}

0 comments on commit 4fdedae

Please sign in to comment.