Spring批量仅限制单个作业实例 [英] Spring batch restrict single instance of job only

查看:107
本文介绍了Spring批量仅限制单个作业实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹簧批处理作业,可以通过休息URL踢。我想确保只允许一个作业实例运行。如果另一个实例已经运行,那么不要启动另一个即使参数不同。

I have one spring batch job which can be kicked of by rest URL. I want to make sure only one job instance is allowed to run. and if another instance already running then don't start another. even if the parameters are different.

我搜索并发现没有任何开箱即用的解决方案。考虑扩展SimpleJobLauncher。检查作业的任何实例是否正在运行。

I searched and found nothing out of box solution. thinking of extending SimpleJobLauncher. to check if any instance of the job running or not.

推荐答案

您可以尝试拦截作业执行,实现JobExecutionListener接口:

You could try to intercept the job execution, implementing the JobExecutionListener interface:

public class MyJobExecutionListener extends JobExecutionListener {

    //active JobExecution, used as a lock.
    private JobExecution _active;

    public void beforeJob(JobExecution jobExecution) {
        //create a lock
        synchronized(jobExecution) {
            if(_active!=null && _active.isRunning()) {
                jobExecution.stop();
            } else {
                _active=jobExecution;
            }
        }
    }

    public void afterJob(JobExecution jobExecution) {
          //release the lock
          synchronized(jobExecution) {
              if(jobExecution==_active) {
                _active=null; 
              }
          }
    }
}

然后,注入Job定义:

And then, inject to the Job definition:

<job id="myJobConfig">
    <listeners>
        <listener ref="myListener"/>
    </listeners>
</job>

这篇关于Spring批量仅限制单个作业实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆