简体   繁体   中英

Order partitioned steps in Spring Batch

I have implemented Spring batch partitioning and I was returning a Map<String, ExecutionContext> result = new HashMap<>(); from partition() method of Partitioner .

My requirement is to start slave steps in specific order( Insertion Order to result map) and that is not happening with above Map .

I tried to use Map<String, ExecutionContext> result = new LinkedHashMap<>(); and that is also not useful - any thread from map gets started randomly.

I am using SimpleAsyncTaskExecutor as executor in master step configuration and concurrencyLimit parallel threads get started. If I set concurrencyLimit=1 , then I want first thread inserted to result map to get started and so on.

How can I implement this ordering?

From Looking at code, along with above step you might need to override StepExecutionSplitter#split, I believe you can extend SimpleStepExecutionSplitter class and implement Split method, in split method you need to change set used for collecting StepExecution from HashSet to LinkedHashSet, if you want to consider restart as well then you might need to consider changing in getContexts method as well.

Below is the snippet to add splitter.

stepBuilders.get("partitionStep")
                .partitioner(slaveStep)
                .partitioner("myParitionerStep", partitioner)
                .taskExecutor(new SimpleThreadPoolTaskExecutor())
                .splitter(new SimpleStepExecutionSplitter(jobRepository, allowStartIfComplete, stepName, partitioner))
                .build()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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