admin管理员组

文章数量:1391947

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="/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.

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.

Share Improve this question asked Jul 9, 2013 at 14:25 muetzemuetze 1611 silver badge16 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Mediator iterate inside copies MessageContext, therefore All changes within the target\sequence do not affect the rest.

You can write your mediator for counting:

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/ponents/lib/ and restart esb

use the manual

By using the messageSequence.iteratorID property,

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'. Each split messages will have a property call "msgNo" as the message count starting from 0

Try solution suggested in this blog-post: 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/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>
                     <pleteCondition>
                        <messageCount min="-1" max="-1"/>
                     </pleteCondition>
                     <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>

By using the below code counter will be ++

<property name="AttachmentCounter" scope="operation" value="0"/>
<iterate expression="$body/ticket/IctAttachments/item" preservePayload="true">
    <target>
        <sequence>
            <property expression=" number(get-property('operation', 'AttachmentCounter') +1)" name="AttachmentCounter" scope="operation"/>
            <log level="custom">
                <property expression="get-property('operation','AttachmentCounter')" name="AttachmentCounter"/>
            </log>
        </sequence>
    </target>
</iterate>

本文标签: javascriptWSO2 ESB Iterate CounterStack Overflow