简体   繁体   English

WSO2 ESB迭代计数器

[英]WSO2 ESB Iterate Counter

I'm using the iterate mediator for saving files. 我正在使用迭代介体来保存文件。 For this I need a counter for the iterations. 为此,我需要一个用于迭代的计数器。 I tried to create an property outside of the iteration and use the script mediator to count the iterations like follows. 我试图在迭代之外创建一个属性,并使用脚本介体来对迭代进行计数,如下所示。

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

The Problem is, that I get the same number after every iteration. 问题是,每次迭代后我得到的数字都是相同的。 Whats the reason for this? 是什么原因呢? Is there a mistake I don't see? 我没有看到错误吗? Maybe there is another way I couldn't find while searching the internet. 也许有另一种我在搜索互联网时找不到的方法。

Mediator iterate inside copies MessageContext, therefore All changes within the target\\sequence do not affect the rest. 介体在副本MessageContext内部iterate ,因此target\\sequence内的所有更改都不会影响其余部分。

You can write your mediator for counting: 您可以编写您的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;
    }
}

here can download the jar , place it wso2esb-4.6.0/repository/components/lib/ and restart esb 在这里可以下载jar ,将其放入wso2esb-4.6.0 / repository / components / lib /并重新启动esb

use the manual 使用手册

By using the messageSequence.iteratorID property, 通过使用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;
  }
}

here it1 in the messageSequence.it1 is the Iterate mediator's 'Iterate Id'. messageSequence.it1中的it1it1中介的“ Iterate Id”。 Each split messages will have a property call "msgNo" as the message count starting from 0 每个拆分消息将有一个属性调用“ msgNo”,因为消息计数从0开始

Try solution suggested in this blog-post: http://bsenduran.blogspot.ru/2015/07/how-to-get-wso2-esb-iterate-mediators.html : 尝试此博客文章中建议的解决方案: 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>                  

Why not just do a count before the iteration occurs ? 为什么不只是在迭代发生之前进行计数?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM