mule - 如何在foreach中添加多个http请求调用|骡子3

标签 mule mulesoft mule-esb

我正在使用分散-聚集来调用具有相同主机和不同路径的许多端点。如何在 For Each 范围内添加 HTTP 请求连接器,它可以循环到不同的路径并将有效负载添加到同一文件?

我的代码如下,我想循环HTTP请求;

<?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"/>
        <scatter-gather doc:name="Scatter-Gather">
            <http:request config-ref="HTTP_Request_Configuration-testt" path="/repo/cat1" method="GET" doc:name="HTTP Request 1"/>
            <http:request config-ref="HTTP_Request_Configuration-testt" path="/repo/cat2" method="GET" doc:name="HTTP Request 2"/>
            <http:request config-ref="HTTP_Request_Configuration-testt" path="/repo/cat3"" method="GET" doc:name="HTTP Request 4"/>

        </scatter-gather>
        <dw:transform-message doc:name="Set JSON">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
        </dw:transform-message>
        <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>

最佳答案

检查下面的示例代码,我们如何在您的场景中使用 foreach。这是在 Mule4 中。我希望你也能在 Mule3 中做到这一点。

我不知道你为什么要使用foreach。我建议使用 Scatter_Gather 而不是 foreach。因为 foreach 会按顺序工作,而 Scatter_Gather 会同时工作。性能方面的并发进程很好。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <flow name="Testing_foreach_stackoverflowFlow" doc:id="95fe6a04-4aca-4106-a0b3-b53afb7be95a" >
        <scheduler doc:name="Scheduler" doc:id="e6a81b1c-43f9-486a-a451-b31bcadbe23f" >
            <scheduling-strategy >
                <fixed-frequency frequency="5" timeUnit="MINUTES"/>
            </scheduling-strategy>
        </scheduler>
        <set-variable value="#[%dw 2.0
&#10;
&#10;output application/json
&#10;
&#10;---
&#10;
&#10;[]]" doc:name="Set Variable" doc:id="7b1bbba6-d63d-4f89-905b-8b9c7d6a7555" variableName="result"/>
        <set-payload value='#[["test1","test2"]]' doc:name="Set Payload" doc:id="1b7ff757-1a42-4291-b759-50632db842d3" />
        <foreach doc:name="For Each" doc:id="2010c075-b487-4977-8a34-0f58adb23a7d" >
            <http:request method="GET" doc:name="Request" doc:id="a0524fe5-2344-46c2-bb03-90660fb8fc04" url='#["http://localhost:8091/" ++ payload]'/>
            <ee:transform doc:name="Transform Message" doc:id="0aca9608-f7a1-4e1a-a246-11830b6f245e" >
                <ee:message >
                </ee:message>
                <ee:variables >
                    <ee:set-variable variableName="result" ><![CDATA[%dw 2.0
output application/json
---
vars.result + payload 
                ]]></ee:set-variable>
                </ee:variables>
            </ee:transform>
            <logger level="INFO" doc:name="payload" doc:id="22bc3348-90f9-4290-bb53-ee4c19af1f18" message="#[payload]"/>
        </foreach>
        <logger level="INFO" doc:name="payload" doc:id="00e70062-6898-42d7-bc50-974f23a4cd93" message="#[payload]"/>
    </flow>
    <flow name="Testing_foreach_stackoverflowFlow1" doc:id="fc055a5c-09a0-4dce-99be-70e863ffb0ff" >
        <http:listener doc:name="Listener" doc:id="298d5fff-c712-4307-895a-7d4faac42eb1" config-ref="api-httpListenerConfig" path="/test1"/>
        <set-payload value='"grapes"' doc:name="Set Payload" doc:id="bfce761e-c63a-4a06-ab9b-f17360d9a7d2" />
    </flow>
    <flow name="Testing_foreach_stackoverflowFlow2" doc:id="12d67768-525e-46b0-91b4-ce9377b0b7d1" >
        <http:listener doc:name="Listener" doc:id="24fc80d9-0f93-4217-a1a2-3802b57ce3cb" config-ref="api-httpListenerConfig" path="/test2"/>
        <set-payload value='"banana"' doc:name="Set Payload" doc:id="01ad0e82-5f50-49a3-9804-10190953b1c0" />
    </flow>
</mule>

enter image description here

关于mule - 如何在foreach中添加多个http请求调用|骡子3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71052269/

相关文章:

java - java :invoke in mule 4之后如何将java对象转换为JSON

java - Odata 与 Olingo 或 Odata4j

mule - 我如何使用 Mule Studio 数据映射器来生成从复杂的 XSD 模式生成的输出?

mule - 在本地 MULE RUN TIME 上禁用 TLS 版本

xml - 删除 XML header 声明并在 DataWeave 2.0 中提供不带前缀的 XML 命名空间

ssl - mule 异步 tls/ssl 入站端点

sockets - ule子上的AMQPConnector-SocketException : Too many open files

mule-studio - Anypoint Studio 7.1启动错误

mule - 如何在设置变量 (Mule 4) 内部验证参数是否存在而不出现值选择器错误?