gpt4 book ai didi

java - 为什么Splitter在Spring Integration中处理集合的项目时直接返回集合?

转载 作者:行者123 更新时间:2023-12-02 01:45:43 25 4
gpt4 key购买 nike

当我阅读《Spring in Action 5》这本书并读到第9章“集成Spring”时,特别是在“Splitters”部分,书中显示可以在分离时直接在Splitter方法中返回集合将集合分成项目,每个项目发送到另一个 channel 进行处理。我读了一遍又一遍,但仍然无法获取它,为什么不先将集合分开,然后将每个项目返回到另一个 channel ?任何人都可以帮助我更清楚地理解它?非常感谢。

书中首先说:

But what if you want to break it down further such that instead of dealing with a List of LineItems,you process each LineItem separately? All you need to do to split the line-item list into multiple messages, one for each line item, is write a method (not a bean) that’s annotated with @Splitter and returns a collection of LineItems.

书中的代码如下所示:

@Splitter(inputChannel="lineItemsChannel",outputChannel="lineItemChannel")
public List<LineItem> lineItemSplitter(List<LineItem> lineItems) {
return lineItems;
}

这本书解释了这段代码:

When a message carrying a payload of List arrives in the channel named lineItemsChannel, it passes into the lineItemSplitter() method.Per the rules of a splitter, the method must return a collection of the items to be split. In this case, you already have a collection of LineItems, so you just return the collection directly. As a result, each LineItem in the collection is published in a message of its own to the channel named lineItemChannel.(from 9.2.5 Splitters)

但是我认为“lineItemSplitter”Splitter方法应该将每个项目返回到outputChannel“lineItemChannel”进行逐一处理(而且我无法想象在我的表达下如何编写这段代码,希望你明白我的想法)。

最佳答案

实际的分割(发送到输出 channel )是由框架完成的。您的拆分器代码返回集合,框架将每个拆分作为离散消息发出。

在更高版本中(自本书编写以来),您现在还可以返回 IteratorIterable。例如,这在 FileSplitter 中非常有用,因为它可以避免将整个文件加载到内存中。

关于java - 为什么Splitter在Spring Integration中处理集合的项目时直接返回集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57458050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com