Emm
Emm

Reputation: 79

Iterating an array using Mule's foreach scope

I would like to iterate through an array and use the value taken from the array to put it within an http inbound endpoint. How would I be able to iterate through this array and take the value from the array to place it as a variable within the http inbound endpoint?

The code that I used to try was:

<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>

and I get the error

Invalid content was found starting with element 'poll'

Upvotes: 2

Views: 6044

Answers (1)

David Dossot
David Dossot

Reputation: 33413

Inbound endpoints are message sources and can not be parametrized the way you're describing.

To achieve your goal, trying a <poll> message source to wrap a foreach that uses http:outbound-endpoint to perform GET (@method) request-response (@exchange-apttern) interactions.

The trick is to bring the results for the HTTP calls back through the foreach, which by default do not do it. The following illustrate a potential approach:

<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>

Upvotes: 4

Related Questions