简体   繁体   中英

How to run spring-batch jobs threadpooled?

I'm starting spring-batch jobs using JobLauncher.run() . Question: how can I threadpool those invocations? So that eg a maximum of 4 job threads may be running concurrently, and any further jobs are just queued?

    @Autowired
    private JobRegistry registry;

    @Autowired
    private JobLauncher launcher;

Job job = registry.getJob("jobname");
launcher.run(job, params); //immediately starts the job

You can set ThreadPoolTaskExecutor as the task executor used by the SimpleJobLauncher (the class that actually launches the jobs). This executor has some properties you can set, especially maxPoolSize.

public JobLauncher createJobLauncher() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(4);
    taskExecutor.setMaxPoolSize(4);
    taskExecutor.afterPropertiesSet();

    SimpleJobLauncher launcher = (SimpleJobLauncher) super.createJobLauncher();
    launcher.setTaskExecutor(taskExecutor);
    return launcher;
}

Please take a look at the docs .

Possibly a duplicate of Spring Batch Multiple Threads and How to set up multi-threading in Spring Batch? .

Update:

I use the following code to start a batch job asynchronously. But I don't know how to limit the maximum number of executions as you mentioned.

TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
taskExecutor.execute(new Runnable() {
    public void run() {
        try {
            jobLauncher.run(importJob, jobParametersBuilder.toJobParameters());
        } catch (Exception e){
            e.printStackTrace();
        }
    }
});

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