I have one Spring Batch job that can potentially be run across multiple servers. I have a listener in place that prevents multiple instances of the job being run at the same time on one server. However, I want to ensure this job can't be run at the same time on multiple servers.
I've searched and found no solution to this problem
I implemented a listener that checks the number of running Job Executions within the spring batch control tables matching a certain name. If there size of the executions exceeds 1, jobExecution of the current job fails. Here's the code:
@Component
public class SingleInstanceListener implements JobExecutionListener {
@Autowired
private JobExplorer explorer;
@Override
public void beforeJob(JobExecution jobExecution) {
String jobName = jobExecution.getJobInstance().getJobName();
Set<JobExecution> executions = explorer.findRunningJobExecutions(jobName);
if(executions.size() > 1) {
jobExecution.stop();
}
}
@Override
public void afterJob(JobExecution jobExecution) {
}
}
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