Scatter-Gather:合并到一张地图

时间:2015-07-29 00:50:08

标签: mule

我需要查询几个表,并且在分散 - 聚集流程的末尾,我希望有一个这种形式的有效负载

{table_1: [ list of results ], table_2: [ list of results ]}

我试过了:

<scatter-gather doc:name="Scatter-Gather">
    <processor-chain>
        <db:select config-ref="dataSrouce" doc:name="Table 1">
            <db:parameterized-query><![CDATA[// my query
        </db:select>

        <set-payload value="#[['table_1': #[payload]]]" doc:name="Set Payload"/>
    </processor-chain>
    <processor-chain>
        <db:select config-ref="dataSrouce" doc:name="Table 2">
            <db:parameterized-query><![CDATA[// my query
        </db:select>

        <set-payload value="#[['table_2': #[payload]]]" doc:name="Set Payload"/>
    </processor-chain>
</scatter-gather>

<set-payload value="#[groovy:payload.inject([:]) {result, entry -&gt; result += entry; result}]" doc:name="Set Payload"/>

但获得例外:

Exception was found for route(s): 1. Message payload is of type: CaseInsensitiveHashMap (org.mule.routing.CompositeRouting
Exception)
  org.mule.routing.CollectAllAggregationStrategy:51 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/routing/Comp
ositeRoutingException.html)

1 个答案:

答案 0 :(得分:0)

在Scatter-Gather的每个分支中放置一个ObjectToString和一个ObjectToJSON Transformer。

然后在Scatter-Gather之后放置一个具有以下值的SetPayload:

{
    "table_1": #[payload[0]],
    "table_2": #[payload[1]]
}

示例:

<scatter-gather doc:name="Scatter-Gather">
    <processor-chain>
        <flow-ref name="Subflow_1" doc:name="Subflow_1"/>
        <object-to-string-transformer doc:name="Object to String"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        </processor-chain>
    <processor-chain>
        <flow-ref name="Subflow_2" doc:name="Subflow_2"/>
        <object-to-string-transformer doc:name="Object to String"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </processor-chain>
</scatter-gather>

<set-payload value="{
    &quot;table_1&quot;: #[payload[0]], 
    &quot;table_2&quot;: #[payload[1]]}" 
    doc:name="Set Payload" mimeType="application/json"/>