简体   繁体   English

Spring批处理多线程ItemWriter

[英]Spring batch multithreaded ItemWriter

I am quite new to Spring batch framework. 我对Spring批处理框架很陌生。

I am currently writing a batch with a reader and a writer. 我目前正在与读者和作家一起编写批处理。

Reader reads from Db and writer writes to a flat file. 读取器从Db读取,写入器将写入平面文件。 The number of records are 1 million. 记录数为100万。 Writing to file takes a lot of time and I want to improve on that. 写入文件需要很多时间,我想对此进行改进。

What is the best way I can achieve multithreading in writer so that write() method runs in parallel? 我可以在writer中实现多线程以使write()方法并行运行的最佳方法是什么?

Note: In @BeforeStep and @AfterStep callbacks, I am writing header and footer of the file. 注意:在@BeforeStep@AfterStep回调中,我正在编写文件的页眉和页脚。 write() method writes the records to file. write()方法将记录写入文件。

EDIT: 编辑:

I have found out that, writing to file isn't taking much time but one of our internal method which does some sort of decryption takes about 500ms for 1 record. 我发现,写入文件并不需要花费很多时间,但是我们内部进行某种解密的一种方法需要1记录大约500毫秒。 And we have 1 million such records. 我们有100万个这样的记录。

Can I improve the performance by doing decryption in multiple threads? 我可以通过在多个线程中进行解密来提高性能吗? I am not able to understand how to improve from here on. 从现在开始,我不知道如何改进。

This is not really a Spring specific question. 这实际上不是Spring特有的问题。 Typically what people do is they implement some kind of streaming. 人们通常要做的是实现某种流。 As in you don't read the entire query then write everything, but instead read little bits one after another, and then pass each bit onto the writer so it can already begin writing before you have finished reading. 就像您不阅读整个查询,而是写所有内容,而是依次读一点,然后将每一位传递给写入器,这样它就可以在完成读取之前就开始写入了。 This is quicker and enables you also to not use as much memory. 这样可以更快,并且还可以减少内存使用量。 For instance if you had 10GB of data to read and write, you could split it up into 10MB queries, instead of reading the whole 10GB. 例如,如果您要读取和写入10GB的数据,则可以将其拆分为10MB的查询,而不是读取整个10GB的数据。 You should read up on streams. 您应该阅读流。 Parallel writes to the same file however will result in no benefit, or reduced performance. 但是,并行写入同一文件将无济于事或降低性能。 You should also be careful not to start too many threads, again this will reduce performance, and unless your threads are really cheap I don't recommend really making more than 2, as some have already mentioned that the performance of most apps are I/O bound, and there is no getting around that, only mitigating effects by buffering/streaming/caching not blocking threads, or anything else you can do in your app. 您还应注意不要启动太多线程,这又会降低性能,除非您的线程确实很便宜,否则我不建议您实际使用2个以上的线程,因为有些人已经提到大多数应用程序的性能是I / O限制,并且没有解决方法,只能通过缓冲/流/缓存来缓解影响,而不阻塞线程,或者您可以在应用中执行的任何其他操作。

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

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