我有一个包含大约200个对象的json对象列表。我想将该列表拆分为较小的列表,其中每个列表每个包含最多20个对象。我想将每个子列表POST到基于HTTP的端点。
<flow name="send-to-next-step" doc:name="send-to-vm-flow">
<vm:inbound-endpoint exchange-pattern="one-way"
path="send-to-next-step-vm" doc:name="VM" />
<!-- received the JSON List payload with 200 objects-->
<!-- TODO do processing here to split the list into sub-lists and call sub-flow for each sub-list
<flow-ref name="send-to-aggregator-sf" doc:name="Flow Reference" />
</flow>
一种可能的方法是我编写一个迭代列表的java组件,并在迭代每个20个对象之后调用子流。有没有更好的方法来实现这个目标?
答案 0 :(得分:6)
如果您的有效负载是Java集合,则Mule foreach 范围内置批处理:http://www.mulesoft.org/documentation/display/current/Foreach
示例:
<foreach batchSize="20">
<json:object-to-json-transformer/>
<http:outbound-endpoint ... />
</foreach>
答案 1 :(得分:3)
您可以使用Groovy collate
方法进行批处理,然后使用foreach
或collection-splitter
,具体取决于您的需求:
<json:json-to-object-transformer returnClass="java.util.List"/>
<set-payload value="#[groovy:payload.collate(20)]"/>
<foreach>
<json:object-to-json-transformer/>
<http:outbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="8082" path="xx"/>
</foreach>
<set-payload value="#[groovy:payload.flatten()]"/>
这会将每批20个对象发送到http端点,然后展平回原始列表。