[英]How to run spring-batch jobs threadpooled?
I'm starting spring-batch
jobs using JobLauncher.run()
. 我正在使用
JobLauncher.run()
启动spring-batch
作业。 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?
因此,例如,最多可以同时运行4个作业线程,并且任何其他作业只是排队?
@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). 您可以将ThreadPoolTaskExecutor设置为SimpleJobLauncher(实际启动作业的类)使用的任务执行程序。 This executor has some properties you can set, especially maxPoolSize.
此执行程序具有一些您可以设置的属性,尤其是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? 可能是Spring Batch Multiple Threads的副本以及如何在Spring Batch中设置多线程? .
。
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();
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.