Spring batch 2.1.8 Run a single instance of a job, when a quartz cron trigger fires

做~自己de王妃 提交于 2019-12-06 05:09:47

Your job should know what file to process before it is executed. E.g. the file name should be passed as job parameter. Remove JobExecutionListener and add StepExecutionListener to access job parameters via StepExecution#getJobParameters(). One job = one file.

Now from your scheduler you want to make sure that only one job is run at one point of time. You can achieve this in two ways:

• Using the async task executor. In this case each time you launch the job, it will not be executed in a background (unless your scheduler does not fire timer events each time in new thread).

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor" ref="taskExecutor" />
</bean>

<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />

• If you use the same job launcher for other jobs, which need to be executed in background, you need to use ThreadPoolTaskExecutor but you can manually check if the job is running:

for (final JobInstance jobInstance : jobExplorer.getJobInstances(jobName(), 0, LOOK_BEHIND_INSTANCES)) {
    for (final JobExecution jobExecution : jobExplorer.getJobExecutions(jobInstance)) {
        final BatchStatus status = jobExecution.getStatus();

        if (status.isRunning()) {
            // Ops:
            break;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!