Simple use example of MULE loop For Each

tool Version
mule-standalone 3.9.0
Anypoint-Studio 6.4.0

写在前面
The example uses For Eachelements to traverse the inbound properties and output them to the page, Flow filters the favicon.ico request, and Loggerprints the MuleMessage body at the beginning, you can observe INBOUND scoped propertiesthe content of part of it, because the For Each of the example is processing this part. A variable is set pageContentto save the content that needs to be output to the page, and finally the variable is returned as payload.
When I first thought about how to append content to pageContent, I used the Variableelement to be placed in the loop field, and then set the value to the following expression.
#[flowVars.pageContent += '计数器值:'+flowVars.counter+',当前key值:'+flowVars.key+',当前value值:'+payload +'\n']
Although the effect was achieved, another local variable was created in order to append the content, but it was not used, but just In order to assign a value to another variable, it is obviously unreasonable. Therefore, the element is finally found Expression Transformer, and the Expression box of the element is used to fill in the expression to achieve the purpose of appending content to the variable.

For Each配置图

Collectionis the target to be traversed, the Counter Variable Namename of the counter variable, and Root Message Variable Namethe name of the variable that holds the source information. Regarding Batch Sizethe instructions, the official description is as follows, but guess it is to speed up the loop processing? (please comment if you know)

(Optional) Enter an integer to indicate the number of elements in each batch that For Each processes. Potentially, these batches promote quicker processing. If greater than one, each batch is treated as a separate Mule message. For example, if a collection has 200 elements and you set the batch size to 50, For Each will iteratively process 4 batches of 50 elements, each as a separate Mule message.
(via Google Translate) (Optional) Enter an integer to indicate the For Each batch The number of elements of the process. These batches may facilitate faster processing. If greater than one, each batch is treated as a separate Mule message. For example, if a collection has 200 elements, and you set the batch size to 50, For Each
will iteratively process 4 batches of 50 elements, each as a separate Mule message.

In AnypointStudio, when the MEL expression is typed #[flowVars., the value of the counter variable you set will appear, which is the convenience of the tool, but there is no display of the key value, that is, AnypointStudio will not prompt the existence flowVars.keyof the existence, but after using the Expressions can indeed be used to get key values, so tooltips are often not comprehensive.

Flow 结构图

xml文档

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="mytestFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/foreach" allowedMethods="GET" doc:name="HTTP"/>
        <expression-filter expression="#[message.inboundProperties.'http.request.uri' != '/favicon.ico']" doc:name="filter favicon ico"/>
        <logger level="INFO" doc:name="print MuleMessage structure"/>
        <set-variable variableName="pageContent" value="#['']" doc:name="pageContent"/>
        <foreach collection="#[message.inboundProperties]"  doc:name="Iterating inbound properties" >
            <expression-transformer expression="#[flowVars.pageContent += '&#35745;&#25968;&#22120;&#20540;&#65306;'+flowVars.counter+'&#65292;&#24403;&#21069;key&#20540;&#65306;'+flowVars.key+'&#65292;&#24403;&#21069;value&#20540;&#65306;'+payload +'\n']" doc:name="Expression"/>
        </foreach>
        <set-payload value="#[flowVars.pageContent]" doc:name="Set Payload"/>
    </flow>
</mule>

运行结果

参考阅读
Collection is MAP traversal method
For Each official documentation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325997584&siteId=291194637