问题
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.
I can't seem to find a similar question or example anywhere. Any ideas? Thanks, /w
Here's a bit of my configuration. I'm using spring-task-3.2 and spring-batch-2.2 with Spring 3.2.
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>
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) {
}
}
}
回答1:
@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" />
回答2:
I think that you should specify a TaskExecutor
implementation in your JobLauncher
to allow it to launch jobs asynchronously, here is a configuration example from Spring Batch Admin
Context :
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor" ref="jobLauncherTaskExecutor" />
</bean>
<task:executor id="jobLauncherTaskExecutor" pool-size="6" rejection-policy="ABORT" />
来源:https://stackoverflow.com/questions/21408653/multiple-spring-3-2-scheduled-tasks-wont-run-in-parallel