我正在使用iterate mediator来保存文件。 为此,我需要一个迭代计数器。我尝试在迭代之外创建一个属性,并使用脚本调解器来计算迭代次数,如下所示。
<property name="AttachmentCounter" value="0"/>
<iterate xmlns:ns="http://org.apache.synapse/xsd" continueParent="true" expression="$body/ticket/IctAttachments/item" id="IctAttachments" sequential="true">
<target>
<sequence>
<script language="js">
<![CDATA[var counter = mc.getProperty("AttachmentCounter");
counter = parseInt(counter) + 1;
mc.setProperty("AttachmentCounter", counter);]]>
</script>
<log>
<property name="AttachmentCounter:" expression="get-property('AttachmentCounter')"/>
</log>
</sequence>
</target>
</iterate>
问题是,我在每次迭代后获得相同的数字。这是什么原因?有没有我看不到的错误? 也许还有另一种我在搜索互联网时找不到的方式。
答案 0 :(得分:4)
Mediator iterate
内部复制MessageContext,因此target\sequence
中的所有更改都不会影响其余部分。
您可以编写mediator
来计算:
public class CountMediators extends AbstractMediator {
private String xpathString = null;
private String uri = null;
private String prefix = null;
@Override
public boolean mediate(MessageContext synCtx) {
SOAPEnvelope envelope = synCtx.getEnvelope();
SynapseXPath expression = null;
List splitElements = null;
try {
expression = new SynapseXPath(xpathString);
if (uri != null && prefix != null)
expression.addNamespace(new NamespaceImpl(uri, prefix));
splitElements = EIPUtils.getMatchingElements(envelope, synCtx, expression);
} catch (JaxenException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
if (splitElements != null)
synCtx.setProperty("count", splitElements.size());
return true;
}
public String getXpathString() {
return xpathString;
}
public void setXpathString(String xpathString) {
this.xpathString = xpathString;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
这里可以下载jar,把它放到wso2esb-4.6.0 / repository / components / lib /并重新启动esb
使用manual
答案 1 :(得分:2)
使用messageSequence.iteratorID
属性
public class IteratorCounter extends AbstractMediator{
@Override
public boolean mediate(MessageContext ctx) {
String msgSeq = (String) ctx.getProperty("messageSequence.it1");
String count = msgSeq.split("/")[0];
ctx.setProperty("msgNo", count);
return true;
}
}
此it1
messageSequence.it1
中的{{1}}是迭代调解员&#39;迭代ID&#39;。每个拆分消息都有一个属性调用&#34; msgNo&#34;作为从0开始的消息计数
答案 2 :(得分:0)
尝试在此博文中建议的解决方案:http://bsenduran.blogspot.ru/2015/07/how-to-get-wso2-esb-iterate-mediators.html:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="count_iterate"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="it_count" value="0" scope="operation"/>
<iterate expression="//symbols/symbol" sequential="true">
<target>
<sequence>
<property name="synapse_it_count" expression="get-property('operation', 'it_count')"/>
<script language="js">var cnt_str = mc.getProperty('synapse_it_count');
var cnt = parseInt(cnt_str);
cnt++;
mc.setProperty('synapse_it_count', cnt.toString());</script>
<property name="it_count" expression="get-property('synapse_it_count')" scope="operation"/>
<aggregate>
<completeCondition>
<messageCount min="-1" max="-1"/>
</completeCondition>
<onComplete expression="//symbol">
<log level="custom">
<property name="number of symbols" expression="get-property('operation','it_count')"/>
</log>
<respond/>
</onComplete>
</aggregate>
</sequence>
</target>
</iterate>
</inSequence>
</target>
<description/>
</proxy>
答案 3 :(得分:0)
为什么不在迭代发生前进行计数?
<property name="counter" scope="default" type="STRING" expression="fn:count($body/ticket/IctAttachments/item)"/>
<log>
<property expression="$ctx:counter" name="counter"/>
</log>