2

我想遍历一个数组并使用从数组中获取的值将它放在一个 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'
4

1 回答 1

4

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

为了实现您的目标,尝试使用<poll>消息源来包装foreach用于http:outbound-endpoint执行GET(@method) request-response(@exchange-apttern) 交互的消息源。

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

<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>
于 2013-09-30T20:29:26.077 回答