Spring Batch documentation says: "Spring Batch will make no attempt to stop them from being run concurrently" (http://static.springsource.org/spring-batch/reference/html-single/index.html).
But I would like to get an error if I try to run a job and there is an instance of that job running. Is it possible?
I am using Spring Batch 2.1.8.
If you're aware that subsequent job will be queued up, you can use TaskExecutor.
Instantiate a task executor with maximum pool size, say 500
<task:executor id="poolTaskExecutor" pool-size="500"/>
ThrottledTaskexecutor
ThrottledTaskExecutor will pass only given number of tasks at a time to above poolTaskExecutor. This class can be found below in spring github, or you can download maven artifact. https://github.com/SpringSource/spring-batch-admin/blob/master/spring-batch-admin-manager/src/main/java/org/springframework/batch/admin/util/ThrottledTaskExecutor.java
Instantiate ThrottledTaskExecutor
<bean id="throttledTaskExecutor"
class="org.springframework.batch.admin.util.ThrottledTaskExecutor">
<property name="taskExecutor" ref="poolTaskExecutor" />
<property name="throttleLimit" value="1"/>
</bean>
ThrottledJobLauncher
<bean id="throttledJobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor" ref="throttledTaskExecutor" />
</bean>
All jobs launched using throttledJobLauncher above will be running only one instance at a time. This is more flexible that you can limit one or multiple jobs with given throttledLimit.
Actually above poolTaskExecutor definition and ThrottledTaskexecutor class is from spring-batch-admin. These are out-of-box feature. You just have to define ThrottledTaskExecutor with limit and include in the jobLauncher of your job.
You could check
jobExecution.getStatus()
as described in
https://stackoverflow.com/a/8711857/1627688
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With