2

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.

4

4 回答 4

4

Mediatoriterate内部复制 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

使用手册

于 2013-07-10T07:51:39.587 回答
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 开始

于 2014-08-04T11:27:51.523 回答
0

为什么不在迭代发生之前进行计数?

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

<log>
  <property expression="$ctx:counter" name="counter"/>
</log>
于 2017-03-27T09:40:02.563 回答
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>                  
于 2017-03-25T13:54:58.857 回答