MULE 循环For Each简单使用例子

工具版本
mule-standalone3.9.0
Anypoint-Studio6.4.0

写在前面
例子使用For Each元素对入站属性进行遍历并且输出到页面上,Flow过滤了favicon.ico请求,一开始使用Logger打印MuleMessage体,可以观察其中INBOUND scoped properties部分的内容,因为例子的For Each正是对该部分进行处理。设置了一个变量pageContent用于保存需要输出到页面的内容,最后将该变量作为payload返回。
在一开始思考如何将内容追加到pageContent时,曾使用Variable元素放在循环域中,然后设置value为下方表达式
#[flowVars.pageContent += '计数器值:'+flowVars.counter+',当前key值:'+flowVars.key+',当前value值:'+payload +'\n']
虽然达到效果,但是为了追加内容而创建另外一个局部变量,却又不使用,反而只是为了给另外一个变量赋值,显然不合常理。因此最后找到了Expression Transformer元素,使用该元素的Expression框填入表达式,达到追加内容到变量中的目的。

For Each配置图

Collection是需要遍历的目标,Counter Variable Name是计数器变量名称,Root Message Variable Name是保存了源信息的变量的名称。关于Batch Size的说明,官方描述如下,可猜测是加快循环处理?(知道的请评论指点一下)

(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 谷歌翻译) (可选)输入一个整数以指示每个批次中For Each进程的元素数量。 这些批次可能会促进更快的处理。 如果大于一个,每个批次都被视为单独的Mule消息。 例如,如果一个集合有200个元素,并且您将批量大小设置为50,则For Each
将迭代处理4批次的50个元素,每个元素作为单独的Mule消息。

在AnypointStudio中,MEL表达式键入#[flowVars.的时候,会出现你所设置counter变量的值,这是工具的便利性,但是并没有key值的显示,即AnypointStudio并不会提示存在flowVars.key的存在,但是经过使用该表达式的确可以用于获取键值,因此往往工具提示也未必全面。

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>

运行结果

参考阅读
集合是MAP遍历方法
For Each官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值