将 3 个不同调用的负载聚合到 1 个文件中 |骡子 3

Aggregate payload of 3 different calls into 1 file | Mule 3

我尝试将三个 http 请求调用的负载聚合到 1 个文件中。我为此使用分散收集。一个 http 请求调用使用一个 http 请求的输出响应,另一个单独调用。问题是我无法将所有三个有效载荷合并到一个文件中。有没有人有这方面的经验。

下面我添加了代码和图片来表达清楚。

这是我想要的文件输出;

{
  "@odata.context": "https://servicenow.com/repo/odata/$metadata#Changes",
  "value": [
    {
       "id": "gggggg-210c-49d7-aa25-ddfdfdfdfd",
      "changenr": "1111111111111111",
      "Description": "xxxxxxxxxxxxx",
    },
    {
      "id": "fffffff-210c-49d7-aa25-ddfdfdfdfd",
      "changenr": "1111111111111111",
      "Description": "xxxxxxxxxxxxx",

    },
    {
      "id": "3k3k3k3-210c-3234-aa25-ddfdfdfdfd",
      "changenr": "1111111111111111",
      "Description": "xxxxxxxxxxxxx",
    }
  ],
  "@odata.context": "https://servicenow.com/repo/odata/$metadata#Details", #### USE THE ID of CHANGEs to call DETAILS
  "value": [
    {
      "id": "7005fc43-9c9d-43f5-abc1-656565cbdd63",
      "detailDesc": "No number present",
      "Date": "2022-01-24T18:59:58Z",
      "status": false,
    },
    {
      "id": "7005fc43-9c9d-43f5-abc1-656565cbdd63",
      "detailDesc": "No number present",
      "Date": "2022-01-24T18:59:58Z",
      "status": true,
    }
    ],
  "@odata.context": "https://servicenow.com/repo/odata/$metadata#Problems", ## SECOND CALL in the scatter
  "value": [
    {
      "id": "9001fc4a-8c8d-43f5-abc7-146945cbdd63",
      "problemnr": "problem with tank",
      "briefDescription": "H1",
      "reporterName": "unknow,",

    },
    {
    "id": "9001fc4a-8c8d-43f5-abc7-146945cbdd63",
      "problemnr": "problem with tank",
      "briefDescription": "H1 and h2",
      "reporterName": "unknow,",
    },
    {
     "id": "9001fc4a-8c8d-43f5-abc7-146945cbdd63",
      "problemnr": "problem with tank",
      "briefDescription": "H1 and h2 & H3",
      "reporterName": "unknow,",
    }
  ]
}

谢谢大家。

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

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:validation="http://www.mulesoft.org/schema/mule/validation" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" 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:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="servicenow.net" port="443" basePath="servicenow/Details?$filter=id eq #[flowVars.arrayCreate]" doc:name="HTTP Request Configuration">
        <http:basic-authentication username="xxxxxxxxxxxxxxx" password="xxxxxxxxxxxxxxxxxxxxx" preemptive="true"/>
    </http:request-config>
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration1" protocol="HTTPS" host="lol.servicenow.net" port="443" basePath="/services/changes?$filter=Date gt 2021-12-01T10:00:00Z and Date lt 2021-12-01T11:00:00Z" doc:name="HTTP Request Configuration">
        <http:basic-authentication username="xxxxxxxxx" password="xxxxxxxxxxxxxxxxxxxx" preemptive="true"/>
    </http:request-config>
    <http:request-config name="HTTP_Request_Configuration2" protocol="HTTPS" host="servicenow.net" port="443" doc:name="HTTP Request Configuration">
        <http:basic-authentication username="xxxxxxxxxxx" password="xxxxxxxxxxxxxxxx" preemptive="true"/>
    </http:request-config>
    <file:connector name="File" autoDelete="true" outputAppend="true" streaming="true" validateConnections="true" doc:name="File"/>
    <file:connector name="File3" writeToDirectory="D:\servicenow" workFileNamePattern="second-of-three-#[function:datestamp:ddMMyyyy-HHmmss].json" autoDelete="true" outputAppend="true" streaming="true" validateConnections="true" doc:name="File"/>
    <file:connector name="File2222" writeToDirectory="D:\servicenow"  autoDelete="true" streaming="true" validateConnections="true" doc:name="File" outputAppend="true"/>
    <flow name="test-filtersFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <scatter-gather doc:name="Scatter-Gather">
            <processor-chain>
                <http:request config-ref="HTTP_Request_Configuration1" path="" method="GET" doc:name="HTTP"/>
                <dw:transform-message doc:name="Transform Message">
                    <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
                    <dw:set-variable variableName="arrayCreate"><![CDATA[%dw 1.0
%output application/java
---
(payload.value.id) joinBy " or id eq "]]></dw:set-variable>
                    <dw:set-variable variableName="incPayload"><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-variable>
                </dw:transform-message>
                <http:request config-ref="HTTP_Request_Configuration" path="" method="GET" doc:name="HTTP"/>
                <dw:transform-message doc:name="Transform Message">
                    <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload ++ flowVars.incPayload]]></dw:set-payload>
                </dw:transform-message>

            </processor-chain>
            <processor-chain>
                <http:request config-ref="HTTP_Request_Configuration2" path="servicenow/problems" method="GET" doc:name="HTTP"/>
                <dw:transform-message doc:name="Transform Message">
                    <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
                    <dw:set-variable variableName="catPayload"><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-variable>
                </dw:transform-message>
            </processor-chain>
        </scatter-gather>
        <file:outbound-endpoint path="D:\fileshare-service" outputPattern="second-of-three-#[function:datestamp:ddMMyyyy-HHmmss].json" responseTimeout="10000" doc:name="File" connector-ref="File2222"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
        </dw:transform-message>
    </flow>
</mule>

Scatter-Gather 收集所有并发路由的响应,并将它们聚合到一条消息中。在您的情况下,只有 TWO 个并发路由,因为一个是顺序路由,其中​​一个 http 请求依赖于另一个。所以你不会有来自 Scatter-Gather 的三个响应数组。您需要提供您想要的输出结果,以便我们为您提供帮助。

Scatter-Gather 的结构如下所示,它是 JAVA 的数组。

{ “0”:{ “属性”: ”....”, “有效载荷”:“” }, “1”:{ “属性”: ”....”, “有效载荷”:“” } }