Search code examples
mulemulesoftmule-esb

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


I try to aggregate the payload of three http request call into 1 file. I use the scatter gather for that. One of http requests call is using the output response of one http request and the other is alone. The issue is that I can not combine al three payload into one file. Does anyone have experience with this.

Below I have added the code and picture to make it clear.

This is my desire output in the file;

{
  "@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,",
    }
  ]
}

Thanks guys.

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

enter image description here


Solution

  • Scatter-Gather collects the responses from all the concurrent routes, and aggregates them into a single message. In your case there is only TWO concurrent routes because one is sequential routes in which one http request is dependent on the other one. so you will not have three response array from Scatter-Gather. You need to provide what is the desired output you want so that we can help you.

    The Structure of the Scatter-Gather will look like below which is array of JAVA .

    { "0": { "attributes": "....", "payload": "" }, "1": { "attributes": "....", "payload": "" } }