多个Spring 3.2计划任务不会并行运行

时间:2014-01-28 14:43:10

标签: java spring scheduled-tasks spring-batch spring-scheduled

我有两个我希望并行运行的计划任务。这两个任务执行Spring Batch作业。每15秒钟一次,另外3秒钟。问题是一个将运行,另一个被阻塞直到它完成。

我似乎无法在任何地方找到类似的问题或例子。有任何想法吗?谢谢,/ w

这是我的一些配置。我在Spring 3.2中使用了spring-task-3.2和spring-batch-2.2。

此批处理作业由mappingRunScheduler运行。另一个任务runScheduler非常相似,没有共享bean。

<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 - 运行批处理作业。

@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) {
        }
    }

}

2 个答案:

答案 0 :(得分:3)

@BoristheSpider指出我的方向正确。需要像这样配置我的任务。将需要调整池大小,但这允许作业并行运行。

<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" />

答案 1 :(得分:2)

我认为您应该在TaskExecutor中指定JobLauncher实现,以允许它以异步方式启动作业,这是Spring Batch Admin上下文中的配置示例:

<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" />