-
Notifications
You must be signed in to change notification settings - Fork 441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Scatter Gather mediator #2231
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ccc7e5
Add scatter gather mediator
SanojPunchihewa 94b201f
Skip adding aggregates when the flow is completed
SanojPunchihewa dad9df5
Refactor scatter-gather synapse syntax
SanojPunchihewa b45b4ca
Add synapse expression support and option to set output to a variable
SanojPunchihewa a85c622
Fix concurrency issue in aggregation
SanojPunchihewa e0c7d37
Fix review comments
SanojPunchihewa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
modules/core/src/main/java/org/apache/synapse/config/xml/ScatterGatherMediatorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.synapse.config.xml; | ||
|
||
import org.apache.axiom.om.OMAttribute; | ||
import org.apache.axiom.om.OMElement; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.synapse.Mediator; | ||
import org.apache.synapse.SynapseException; | ||
import org.apache.synapse.mediators.eip.Target; | ||
import org.apache.synapse.mediators.v2.ScatterGather; | ||
import org.jaxen.JaxenException; | ||
|
||
import java.util.Iterator; | ||
import java.util.Properties; | ||
import javax.xml.namespace.QName; | ||
|
||
/** | ||
* The <scatter-gather> mediator is used to copy messages in Synapse to similar messages but with | ||
* different message contexts and aggregate the responses back. | ||
* | ||
* <pre> | ||
* <scatter-gather parallel-execution=(true | false) result-target=(body | variable) content-type=(JSON | XML)> | ||
* <aggregation value="expression" condition="expression" timeout="long" | ||
* min-messages="expression" max-messages="expression"/> | ||
* <sequence> | ||
* (mediator)+ | ||
* </sequence>+ | ||
* </scatter-gather> | ||
* </pre> | ||
*/ | ||
public class ScatterGatherMediatorFactory extends AbstractMediatorFactory { | ||
|
||
/** | ||
* This will hold the QName of the clone mediator element in the xml configuration | ||
*/ | ||
private static final QName SCATTER_GATHER_Q | ||
= new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "scatter-gather"); | ||
private static final QName ELEMENT_AGGREGATE_Q | ||
= new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "aggregation"); | ||
private static final QName ATT_AGGREGATE_EXPRESSION = new QName("expression"); | ||
private static final QName ATT_CONDITION = new QName("condition"); | ||
private static final QName ATT_TIMEOUT = new QName("timeout"); | ||
private static final QName ATT_MIN_MESSAGES = new QName("min-messages"); | ||
private static final QName ATT_MAX_MESSAGES = new QName("max-messages"); | ||
private static final QName SEQUENCE_Q = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "sequence"); | ||
private static final QName PARALLEL_EXEC_Q = new QName("parallel-execution"); | ||
private static final QName RESULT_TARGET_Q = new QName("result-target"); | ||
private static final QName ROOT_ELEMENT_Q = new QName("root-element"); | ||
private static final QName CONTENT_TYPE_Q = new QName("content-type"); | ||
|
||
private static final SequenceMediatorFactory fac = new SequenceMediatorFactory(); | ||
|
||
public Mediator createSpecificMediator(OMElement elem, Properties properties) { | ||
|
||
boolean asynchronousExe = true; | ||
|
||
ScatterGather mediator = new ScatterGather(); | ||
processAuditStatus(mediator, elem); | ||
|
||
OMAttribute parallelExecAttr = elem.getAttribute(PARALLEL_EXEC_Q); | ||
if (parallelExecAttr != null && parallelExecAttr.getAttributeValue().equals("false")) { | ||
asynchronousExe = false; | ||
} | ||
mediator.setParallelExecution(asynchronousExe); | ||
|
||
OMAttribute contentTypeAttr = elem.getAttribute(CONTENT_TYPE_Q); | ||
if (contentTypeAttr == null || StringUtils.isBlank(contentTypeAttr.getAttributeValue())) { | ||
String msg = "The 'content-type' attribute is required for the configuration of a Scatter Gather mediator"; | ||
throw new SynapseException(msg); | ||
} else { | ||
if ("JSON".equals(contentTypeAttr.getAttributeValue())) { | ||
mediator.setContentType(ScatterGather.JSON_TYPE); | ||
} else if ("XML".equals(contentTypeAttr.getAttributeValue())) { | ||
OMAttribute rootElementAttr = elem.getAttribute(ROOT_ELEMENT_Q); | ||
if (rootElementAttr != null && StringUtils.isNotBlank(rootElementAttr.getAttributeValue())) { | ||
mediator.setRootElementName(rootElementAttr.getAttributeValue()); | ||
mediator.setContentType(ScatterGather.XML_TYPE); | ||
} else { | ||
String msg = "The 'root-element' attribute is required for the configuration of a " + | ||
"Scatter Gather mediator when the 'content-type' is 'XML'"; | ||
throw new SynapseException(msg); | ||
} | ||
} else { | ||
String msg = "The 'content-type' attribute should be either 'JSON' or 'XML'"; | ||
throw new SynapseException(msg); | ||
} | ||
} | ||
|
||
OMAttribute resultTargetAttr = elem.getAttribute(RESULT_TARGET_Q); | ||
if (resultTargetAttr == null || StringUtils.isBlank(resultTargetAttr.getAttributeValue())) { | ||
String msg = "The 'result-target' attribute is required for the configuration of a Scatter Gather mediator"; | ||
throw new SynapseException(msg); | ||
} else { | ||
mediator.setResultTarget(resultTargetAttr.getAttributeValue()); | ||
} | ||
|
||
Iterator sequenceListElements = elem.getChildrenWithName(SEQUENCE_Q); | ||
if (!sequenceListElements.hasNext()) { | ||
String msg = "A 'sequence' element is required for the configuration of a Scatter Gather mediator"; | ||
throw new SynapseException(msg); | ||
} | ||
while (sequenceListElements.hasNext()) { | ||
OMElement sequence = (OMElement) sequenceListElements.next(); | ||
if (sequence != null) { | ||
Target target = new Target(); | ||
target.setSequence(fac.createAnonymousSequence(sequence, properties)); | ||
target.setAsynchronous(asynchronousExe); | ||
mediator.addTarget(target); | ||
} | ||
} | ||
|
||
OMElement aggregateElement = elem.getFirstChildWithName(ELEMENT_AGGREGATE_Q); | ||
if (aggregateElement != null) { | ||
OMAttribute aggregateExpr = aggregateElement.getAttribute(ATT_AGGREGATE_EXPRESSION); | ||
if (aggregateExpr != null) { | ||
try { | ||
mediator.setAggregationExpression( | ||
SynapsePathFactory.getSynapsePath(aggregateElement, ATT_AGGREGATE_EXPRESSION)); | ||
} catch (JaxenException e) { | ||
handleException("Unable to load the aggregating expression", e); | ||
} | ||
} else { | ||
String msg = "The 'expression' attribute is required for the configuration of a Scatter Gather mediator"; | ||
throw new SynapseException(msg); | ||
} | ||
|
||
OMAttribute conditionExpr = aggregateElement.getAttribute(ATT_CONDITION); | ||
if (conditionExpr != null) { | ||
try { | ||
mediator.setCorrelateExpression( | ||
SynapsePathFactory.getSynapsePath(aggregateElement, ATT_CONDITION)); | ||
} catch (JaxenException e) { | ||
handleException("Unable to load the condition expression", e); | ||
} | ||
} | ||
|
||
OMAttribute completeTimeout = aggregateElement.getAttribute(ATT_TIMEOUT); | ||
if (completeTimeout != null) { | ||
mediator.setCompletionTimeoutMillis(Long.parseLong(completeTimeout.getAttributeValue())); | ||
} | ||
|
||
OMAttribute minMessages = aggregateElement.getAttribute(ATT_MIN_MESSAGES); | ||
if (minMessages != null) { | ||
mediator.setMinMessagesToComplete(new ValueFactory().createValue("min-messages", aggregateElement)); | ||
} | ||
|
||
OMAttribute maxMessages = aggregateElement.getAttribute(ATT_MAX_MESSAGES); | ||
if (maxMessages != null) { | ||
mediator.setMaxMessagesToComplete(new ValueFactory().createValue("max-messages", aggregateElement)); | ||
} | ||
} else { | ||
String msg = "The 'aggregation' element is required for the configuration of a Scatter Gather mediator"; | ||
throw new SynapseException(msg); | ||
} | ||
addAllCommentChildrenToList(elem, mediator.getCommentsList()); | ||
return mediator; | ||
} | ||
|
||
/** | ||
* This method will implement the getTagQName method of the MediatorFactory interface | ||
* | ||
* @return QName of the clone element in xml configuration | ||
*/ | ||
public QName getTagQName() { | ||
|
||
return SCATTER_GATHER_Q; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...les/core/src/main/java/org/apache/synapse/config/xml/ScatterGatherMediatorSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.synapse.config.xml; | ||
|
||
import org.apache.axiom.om.OMElement; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.synapse.Mediator; | ||
import org.apache.synapse.mediators.eip.Target; | ||
import org.apache.synapse.mediators.v2.ScatterGather; | ||
|
||
/** | ||
* Serializer for {@link ScatterGather} instances. | ||
*/ | ||
public class ScatterGatherMediatorSerializer extends AbstractMediatorSerializer { | ||
|
||
public OMElement serializeSpecificMediator(Mediator m) { | ||
|
||
ScatterGather scatterGatherMediator = null; | ||
if (!(m instanceof ScatterGather)) { | ||
handleException("Unsupported mediator passed in for serialization : " + m.getType()); | ||
} else { | ||
scatterGatherMediator = (ScatterGather) m; | ||
} | ||
|
||
assert scatterGatherMediator != null; | ||
OMElement scatterGatherElement = fac.createOMElement("scatter-gather", synNS); | ||
saveTracingState(scatterGatherElement, scatterGatherMediator); | ||
|
||
scatterGatherElement.addAttribute(fac.createOMAttribute( | ||
"parallel-execution", nullNS, Boolean.toString(scatterGatherMediator.getParallelExecution()))); | ||
scatterGatherElement.addAttribute(fac.createOMAttribute( | ||
"result-target", nullNS, scatterGatherMediator.getResultTarget())); | ||
scatterGatherElement.addAttribute(fac.createOMAttribute( | ||
"content-type", nullNS, scatterGatherMediator.getContentType())); | ||
if (StringUtils.isNotBlank(scatterGatherMediator.getRootElementName())) { | ||
scatterGatherElement.addAttribute(fac.createOMAttribute( | ||
"root-element", nullNS, scatterGatherMediator.getRootElementName())); | ||
} | ||
|
||
OMElement aggregationElement = fac.createOMElement("aggregation", synNS); | ||
|
||
SynapsePathSerializer.serializePath( | ||
scatterGatherMediator.getAggregationExpression(), aggregationElement, "expression"); | ||
|
||
if (scatterGatherMediator.getCorrelateExpression() != null) { | ||
SynapsePathSerializer.serializePath( | ||
scatterGatherMediator.getAggregationExpression(), aggregationElement, "condition"); | ||
} | ||
|
||
if (scatterGatherMediator.getCompletionTimeoutMillis() != 0) { | ||
aggregationElement.addAttribute(fac.createOMAttribute( | ||
"timeout", nullNS, Long.toString(scatterGatherMediator.getCompletionTimeoutMillis()))); | ||
} | ||
if (scatterGatherMediator.getMinMessagesToComplete() != null) { | ||
new ValueSerializer().serializeValue( | ||
scatterGatherMediator.getMinMessagesToComplete(), "min-messages", aggregationElement); | ||
} | ||
if (scatterGatherMediator.getMaxMessagesToComplete() != null) { | ||
new ValueSerializer().serializeValue( | ||
scatterGatherMediator.getMaxMessagesToComplete(), "max-messages", aggregationElement); | ||
} | ||
scatterGatherElement.addChild(aggregationElement); | ||
|
||
for (Target target : scatterGatherMediator.getTargets()) { | ||
if (target != null && target.getSequence() != null) { | ||
SequenceMediatorSerializer serializer = new SequenceMediatorSerializer(); | ||
serializer.serializeAnonymousSequence(scatterGatherElement, target.getSequence()); | ||
} | ||
} | ||
serializeComments(scatterGatherElement, scatterGatherMediator.getCommentsList()); | ||
|
||
return scatterGatherElement; | ||
} | ||
|
||
public String getMediatorClassName() { | ||
|
||
return ScatterGather.class.getName(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed to derive this from the message context, and make this optional right? Was that changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are creating a new message after aggregating the responses we cannot derive this. Since the new synapse expression return a JSON object from XML payload and vice versa, the new message will be converted to the given content type. Further we need to have a root element to append XML payload, Hence we will need to have this content type parameter.