我创建了许多 ArrayList 来保存要拆分为各自实体的单独实体的集合。这些被添加到一个 ArrayList 中,然后推送到我尝试拆分列表列表的交换中。我似乎无法拆分列表列表。
尝试了 Splitter 的多种变体,包括 token 等。
List<String> serviceRecords = new ArrayList<String>(); //holds an assets collection
List<String> toRecords = new ArrayList<String>(); //holds all asset collections
收集属于某个组的项目后,它们将被添加到 ArrayList 中。
serviceRecords.add(sb.toString());
添加所有项目后,列表将添加到列表中。
toRecords.addAll(serviceRecords);
然后我将列表推送到交易所
exchange.getIn().setBody(toRecords);
然后是 XML 路线,它在尝试不同的事情时处于困惑状态。这些都不起作用。拆分列表列表一定有技巧。
<route autoStartup="true" id="core.fleet.asset.splitterRoute">
<from id="_from4" uri="{{fileEnrichmentEndpoint}}"/>
<process id="_process4" ref="assetCollectorProcessor"/>
<process id="_process5" ref="fleetAssetSplitter"/>
<split id="FleetSplit"> <!-- strategyRef="tsAggregationStrategy" -->
<!-- <simple>${body}</simple> -->
<tokenize token="BLOCKMarker"/>
<log id="splitBody" message="Split Body: ${body}"/>
<to id="_splitOut" uri="{{fileSplitDestination}}/?fileName=GCMS_asset_${date:now:yyyyMMddhhmmss}.csv"/>
<!-- <process id="getName" ref="fleetAssetFileName"/> -->
<!-- <to id="_splitOut" uri="{{fileSplitDestination}}/?fileName=GCMS_asset_${date:now:yyyyMMdd}.csv"/> -->
</split>
<!-- <to id="_splitOut" uri="{{fileSplitDestination}}/?fileName=GCMS_asset_${date:now:yyyyMMdd}.csv"/> -->
<stop/>
</route>
通过列表列表的拆分生成多个文件。
最佳答案
您可以根据需要使用 JavaBean 来实现嵌套的 Splitter 逻辑。
如果标准Splitter EIP不能做你想做的事,你可以用一个bean告诉他如何分割你的消息的有效负载。
因此,如果您将 Splitter 配置为使用自定义 bean
from("direct:split")
.log("Original body: ${body}")
.split().method(new SplitterBean(), "splitServiceRecords")
.log("Message part: ${body}")
.to("mock:end");
bean 只是收集结果列表中的所有服务记录(来自您的示例)
public List<String> splitServiceRecords(List<List<String>> body) {
List<String> collectedServiceRecords = new ArrayList<String>();
// iterate over List containing lists and add each item of
// the inner list to the Splitter result
for (List<String> serviceRecords : body) {
for (String serviceRecord : serviceRecords) {
collectedServiceRecords.add(serviceRecord);
}
}
return collectedServiceRecords;
}
列表列表将成为包含所有项目的单个列表,并且每个项目都成为由 Camel 进一步路由的消息。
上面路由的日志输出:
INFO: Original body: [[Foo, Bar, Baz], [Record, item, thing]]
...
INFO: Message part: Foo
INFO: Message part: Bar
INFO: Message part: Baz
INFO: Message part: Record
INFO: Message part: item
INFO: Message part: thing
关于java - 使用驼峰分割器分割列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54470343/