使用Mule的foreach范围迭代数组

时间:2013-09-30 20:27:51

标签: foreach mule

我想迭代一个数组,并使用从数组中获取的值将其放入http入站端点。我如何能够遍历此数组并从数组中获取值以将其作为变量置于http入站端点中?

我以前尝试的代码是:

<flow name="foreachFlow1" doc:name="foreachFlow1">
    <poll frequency="2000">
    <foreach collection="#[groovy:['localhost:8082', 'localhost:8083']]"
        doc:name="For Each">
        <http:outbound-endpoint exchange-pattern="request-response"
            address="http://#[payload]" method="GET" doc:name="HTTP" />
    </foreach>
    </poll>
</flow>

我收到了错误

Invalid content was found starting with element 'poll'

1 个答案:

答案 0 :(得分:4)

入站端点是消息源,无法按照您描述的方式进行参数化。

要实现您的目标,请尝试使用<poll>消息来包裹foreach使用http:outbound-endpoint执行GET(@method)request-response的{​​{1}}(@交换 - 交流。互动。

诀窍是通过foreach将HTTP调用的结果带回来,默认情况下不会这样做。以下说明了一种潜在的方法:

<flow name="foreachFlow1">
    <poll frequency="2000">
        <processor-chain>
            <set-variable variableName="httpResponses" value="#[[]]" />
            <foreach collection="#[groovy:['localhost:8082', 'localhost:8083']]">
                <http:outbound-endpoint
                    exchange-pattern="request-response" address="http://#[payload]"
                    method="GET" />
                <expression-component>httpResponses.add(message.payloadAs(java.lang.String))
                </expression-component>
            </foreach>
        </processor-chain>
    </poll>
    <logger level="INFO" message="#[httpResponses]" />
</flow>

<!-- Test server stubs -->

<flow name="server8082">
    <http:inbound-endpoint exchange-pattern="request-response"
        address="http://localhost:8082" />
    <set-payload value="This is 8082" />
</flow>

<flow name="server8083">
    <http:inbound-endpoint exchange-pattern="request-response"
        address="http://localhost:8083" />
    <set-payload value="This is 8083" />
</flow>