Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

java - spring batch multithread same sort in reader as writer

We are experimenting with a spring batch single thread to spring batch multithread.

The setup is rather easy :

  • Reader : Read some data with a specific sorting.
  • Processor : fetch some additional values.
  • Writer : Write it all to an csv file in order of read.

So we have changed the reader to a JdbcPagingItemReader and converted the sort from

order by firstname, lastname, id;

to

Map<String, Order> sortConfiguration = new HashMap<>();
sortConfiguration.put("firstname", Order.ASCENDING);
sortConfiguration.put("lastname", Order.ASCENDING);
sortConfiguration.put("id", Order.ASCENDING);

the commit-interval is set to 200.
The batch runs fine, but our csv is completly out of order.
I assumed that spring would write in the file after each commit (and hoped that he write page per page in order), but the disorder is greater then chunks 200 lines.
I got for example line 1, 3 and 5 should be together in a thread and line 2 and 4 in another thread.
Is there any option to preserve the order or is the only way to abandon multithread?

question from:https://stackoverflow.com/questions/65916086/spring-batch-multithread-same-sort-in-reader-as-writer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Multi-threading is incompatible with ordering. If you use a multi-threaded step, items will be read, processed and written in an undefined order. This is mentioned in the Multi-threaded Step section of the reference documentation:

The result of the above configuration is that the Step executes by reading, processing,
and writing each chunk of items (each commit interval) in a separate thread of execution.
Note that this means there is no fixed order for the items to be processed, and a chunk
might contain items that are non-consecutive compared to the single-threaded case.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...