简体   繁体   English

如何在spring-batch中创建stream.parallel ItemReader?

[英]How to create a stream.parallel ItemReader in spring-batch?

How can I use the new java 8 streams.parallel api in spring-batch ItemReader ? 如何在spring-batch ItemReader使用新的Java 8 stream.parallel api?

list.stream().parallel().forEach(file -> readLines(file));

Typically, an ItemReader reads all lines from a file up to a defined chunk size, then passes the chunked list to an itemwriter. 通常,ItemReader从文件读取所有行,直到定义的块大小为止,然后将块列表传递给Itemwriter。 And when the writer finished, the next chunk is read. 当编写器完成时,将读取下一个块。

I want this process of reading+writing to be using the java8 stream.parallel api. 我希望此读写过程使用java8 stream.parallel api。 Is that possible? 那可能吗?

Within the reader itself, I'm not 100% sure that use case makes sense. 在读者本身内部,我不确定百分百用例是否有意义。 The idea of the stream api is to stream from a source to a terminal operation. 流api的思想是从源流式传输到终端操作。 The contract for a Spring Batch ItemReader is to provide items one at a time which obviously isn't ideal. Spring Batch ItemReader的合同是一次提供一个项目,这显然不理想。 I think the closest you could get is to use the stream api to load the items into memory, but there is no way to stop it from reading between items or even chunks. 我认为您可以找到的最接近的方法是使用流api将项目加载到内存中,但是无法阻止它在项目之间甚至块之间读取。

I've been looking at the idea of creating a Step implementation that uses a stream concept (either Java 8 streams or reactive streams via Reactor or RxJava) but that won't be available until the 3.1 release at the earliest (I'd be open to working it with you if you're interested). 我一直在研究创建一个使用流概念(Java 8流或通过Reactor或RxJava的反应流)的Step实现的想法,但要等到最早的3.1版本发布后才能使用(我会如果您有兴趣,可以与您合作)。 That would use a stream provider of some kind as the "reader", the intermediate functions serve the role "processor", and a terminal function serves as the "writer". 这将使用某种类型的流提供程序作为“读取器”,中间功能充当“处理器”角色,而终端功能充当“编写器”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM