2013-07-09 95 views
2

我正在使用迭代調解器來保存文件。 爲此,我需要一個迭代計數器。我試圖在迭代之外創建一個屬性,並使用腳本中介來計算迭代,如下所示。WSO2 ESB迭代計數器

<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> 

問題是,我在每次迭代後都得到相同的數字。這是什麼原因?有沒有我看不到的錯誤? 也許在搜索互聯網時找不到另一種方式。

回答

4

調解器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 /存儲庫/組件/ lib目錄/重啓ESB

使用manual

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; 
    } 
} 

這裏it1messageSequence.it1是迭代中介的'迭代Id'。每個拆分的消息將有一個屬性調用「msgNo」從0

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>     
0

爲什麼不只是做一個計數的重複發生之前?

<property name="counter" scope="default" type="STRING" expression="fn:count($body/ticket/IctAttachments/item)"/> 

<log> 
    <property expression="$ctx:counter" name="counter"/> 
</log>