3

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
membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

4

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;
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120
0

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();
        }
    }
});
Community
  • 1
  • 1
kenda
  • 488
  • 6
  • 16
  • 1
    The linked question is about multi threaded **steps**, which is *not* what I asked for. – membersound Jul 11 '16 at 12:32
  • SimpleAsyncTaskExecutor only runs the workload in a separate thread. Basically, it fires a new thread for the job it runs, it doesn't run it synchronously waiting for the answer. – Alexandru Marina Jul 11 '16 at 12:54