You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mule 3中如何在For Each作用域内添加多个HTTP请求调用

Refactoring to Use For Each for HTTP Request Loops in Mule 3

Got it, let's adjust your flow to replace the Scatter-Gather with a For Each scope that loops through your target endpoints, collects all responses, and writes them to a single file. Here's the modified implementation and breakdown:

Modified Mule Flow Code

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 
      xmlns:ftp="http://www.mulesoft.org/schema/mule/ee/ftp" 
      xmlns:http="http://www.mulesoft.org/schema/mule/http" 
      xmlns:file="http://www.mulesoft.org/schema/mule/file" 
      xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" 
      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.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
                          http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd 
                          http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd 
                          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/ee/ftp http://www.mulesoft.org/schema/mule/ee/ftp/current/mule-ftp-ee.xsd 
                          http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

    <http:request-config name="HTTP_Request_Configuration-testt" protocol="HTTPS" host="servicenow.com" port="443" doc:name="HTTP Request Configuration"/>
    <http:listener-config name="HTTP_Listener_Configuration1" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

    <flow name="servicenow-call">
        <http:listener config-ref="HTTP_Listener_Configuration1" path="/test" doc:name="HTTP"/>
        
        <!-- Step 1: Define the list of endpoints to loop through -->
        <dw:transform-message doc:name="Define Endpoint Paths">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
["/repo/cat1", "/repo/cat2", "/repo/cat3"]]]></dw:set-payload>
        </dw:transform-message>

        <!-- Step 2: Initialize a variable to collect all responses -->
        <set-variable variableName="allResponses" value="#[[]]" doc:name="Initialize Responses Collection"/>

        <!-- Step 3: Loop through each endpoint path -->
        <foreach doc:name="Loop Through Endpoints">
            <!-- Dynamic HTTP request using the current path from the loop -->
            <http:request config-ref="HTTP_Request_Configuration-testt" path="#[payload]" method="GET" doc:name="HTTP Request"/>
            
            <!-- Add the current response to the collection variable -->
            <set-variable variableName="allResponses" value="#[vars.allResponses + payload]" doc:name="Collect Response"/>
        </foreach>

        <!-- Step 4: Transform the collected responses to JSON -->
        <dw:transform-message doc:name="Convert to JSON">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
vars.allResponses]]></dw:set-payload>
        </dw:transform-message>

        <!-- Step 5: Write the combined JSON to a file -->
        <file:outbound-endpoint path="D:\Local\NewFile.json" outputPattern="first-#[function:datestamp:ddMMyyyy-HHmmss].json" responseTimeout="10000" doc:name="Write File"/>
        
        <exception-strategy ref="ImplementationChoice_Exception_Strategy" doc:name="Reference Exception Strategy"/>
    </flow>
</mule>

Key Changes Explained

  • Endpoint List Definition: We start by creating a Java array of the target paths using DataWeave. This gives us a clear, maintainable list to loop through—easy to add/remove paths later.
  • Response Collection Variable: We initialize an empty array variable allResponses to store each HTTP response as we work through the loop.
  • For Each Scope: This iterates over each path in our list. Inside the loop, the HTTP request uses #[payload] (which is the current path from the iteration) to dynamically set the endpoint.
  • Collecting Responses: After each HTTP request, we append the response payload to our allResponses variable. Using vars.allResponses + payload ensures we build up a single array of all results.
  • Final Transformation: We convert the entire collection of responses to JSON before writing to the file, so you end up with a single JSON array containing all your endpoint results.

Also, I fixed a typo in your original code: the third HTTP request had an extra quote in the path ("/repo/cat3""), which would have caused a syntax error.

内容的提问来源于stack exchange,提问作者Makavelines

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 11:52:29