多个 Spring 3.2 计划任务不会并行运行 [英] Multiple Spring 3.2 scheduled tasks won't run in parallel
问题描述
我有两个计划任务要并行运行.这两个任务执行一个 Spring Batch 作业.每 15 秒一次,另一次每 3 秒一次.问题是一个将运行,另一个被阻塞,直到它完成.
I have two scheduled tasks that I want to run in parallel. These two tasks execute a Spring Batch job. One every 15 seconds and the other ever 3 seconds. The problem is one will be running and the other is blocked until it finishes.
我似乎在任何地方都找不到类似的问题或示例.有任何想法吗?谢谢,/w
I can't seem to find a similar question or example anywhere. Any ideas? Thanks, /w
这是我的一些配置.我在 Spring 3.2 中使用 spring-task-3.2 和 spring-batch-2.2.
Here's a bit of my configuration. I'm using spring-task-3.2 and spring-batch-2.2 with Spring 3.2.
此批处理作业由 mappingRunScheduler
运行.另一个任务 runScheduler
非常相似,没有共享 bean.
This batch job is ran by the mappingRunScheduler
. The other task runScheduler
is very similar and no beans are shared.
<job id="mappingJob" xmlns="http://www.springframework.org/schema/batch">
<step id="mappingStep1" next="mappingStep2">
<tasklet>
<chunk reader="mappingItemReader"
processor="mappingItemProcessor"
writer="mappingItemWriter" commit-interval="1" />
</tasklet>
</step>
<step id="mappingStep2">
<tasklet ref="mappingTasklet" />
</step>
</job>
<bean id="mappingTasklet"
class="com.example.repository.batch.MappingTasklet"
scope="prototype">
<constructor-arg index="0" ref="mappingLister" />
</bean>
<bean id="mappingItemWriter"
class="com.example.repository.batch.MappingItemWriter" scope="prototype" />
<bean id="mappingLister"
class="com.example.repository.batch.MappingLister"
scope="prototype">
<constructor-arg index="0" ref="liveRepositoryService" />
</bean>
<bean id="mappingItemProcessor"
class="com.example.repository.batch.MappingItemProcessor"
scope="prototype">
<constructor-arg index="0" ref="liveRepositoryService" />
</bean>
<bean id="mappingItemReader"
class="com.example.repository.batch.MappingItemReader" scope="step">
<constructor-arg index="0">
<value>#{mappingLister.getModifiedList()}</value>
</constructor-arg>
</bean>
<bean id="mappingRunScheduler"
class="com.example.repository.batch.MappingRunScheduler" />
<task:scheduled-tasks>
<task:scheduled ref="runScheduler"
method="run" cron="*/15 * * * * *" />
<task:scheduled ref="mappingRunScheduler"
method="run" cron="*/3 * * * * *" />
</task:scheduled-tasks>
MappingRunScheduler
- 运行批处理作业.
The MappingRunScheduler
- runs the batch job.
@Service(value = "mappingRunScheduler")
@Component
public class MappingRunScheduler {
public MappingRunScheduler() {
}
public void run() {
try {
String uuid = new Date().toString() + "-"
+ UUID.randomUUID().toString();
JobParameters param = new JobParametersBuilder()
.addString("uuid", uuid)
.addString("job", "mapper").toJobParameters();
JobExecution execution = jobLauncher.run(job, param);
} catch (JobExecutionAlreadyRunningException
| JobRestartException
| JobParametersInvalidException ex) {
} catch (JobInstanceAlreadyCompleteException ex) {
}
}
}
推荐答案
@BoristheSpider 为我指明了正确的方向.需要像这样配置我的任务.需要调整池大小,但这允许作业并行运行.
@BoristheSpider pointed me in the right direction. Needed to configure my tasks like this. Will need to tune the pool size, but this allows the jobs to run in parrallel.
<task:scheduled-tasks scheduler="batchScheduler">
<task:scheduled ref="runScheduler"
method="run" cron="*/15 * * * * *" />
<task:scheduled ref="mappingRunScheduler"
method="run" cron="*/3 * * * * *" />
</task:scheduled-tasks>
<task:scheduler id="batchScheduler" pool-size="100" />
这篇关于多个 Spring 3.2 计划任务不会并行运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!