春季批次:单个工作实例侦听器

发布于 2025-02-02 15:04:17 字数 2572 浏览 3 评论 0原文

我想确保仅允许一个工作实例运行。如果另一个实例已经运行,请停止它。 因此,我实施了一个听众,该听众检查以下操作的数量:(如果是正确的停止工作,我不是SUR)

public class SingleJobInstanceListener implements JobExecutionListener {

    @Override
    public void beforeJob(JobExecution jobExecution) {
        final String jobName = jobExecution.getJobInstance().getJobName();
        LOGGER.info("Listener to check one {} job instance is running", jobName);
        if (Constants.JOB_NAME.equals(jobName)) {
            final Set<JobExecution> executionSet = jobExplorer.findRunningJobExecutions(jobName);
            if (executionSet.size() > 1) {
                for (JobExecution execution : executionSet) {
                        execution.stop();
                        LOGGER.info("{} job instance {} is stopped", jobName, execution.getJobInstance().getInstanceId());
                }
            }
        }
    }
    
}

运行工作:

 @Scheduled(cron = "0/10 * * * * *")
 public void runSpringBatchJob() {
     LOGGER.info("Job was started");
     final JobExecution streamExecution = jobLauncher.run(job, newExecution());
     LOGGER.info("Exit data job with status: {}", streamExecution.getStatus());
     LOGGER.info("Exit data job ID: {}", streamExecution.getJobId());
     LOGGER.info("-----------------------------------------------------");
}

日志:

SpringBatchJobLauncher       : Job was started
SingleJobInstanceListener    : Listener to check one testJob job instance is running
SingleJobInstanceListener    : testJob job instance 178 is stopped
SingleJobInstanceListener    : testJob job instance 160 is stopped
SingleJobInstanceListener    : testJob job instance 154 is stopped
StreamWriter                 : Writing data: [......]
SpringBatchJobLauncher       : Exit data job with status: COMPLETED
SpringBatchJobLauncher       : Exit data job ID: 178
SpringBatchJobLauncher       : -----------------------------------------------------
SpringBatchJobLauncher       : Job was started
SingleJobInstanceListener    : Listener to check one testJob job instance is running
SingleJobInstanceListener    : testJob job instance 160 is stopped
SingleJobInstanceListener    : testJob job instance 179 is stopped
SingleJobInstanceListener    : testJob job instance 154 is stopped
StreamWriter                 : Writing data: [......]
SpringBatchJobLauncher       : Exit data job with status: COMPLETED
SpringBatchJobLauncher       : Exit data job ID: 179

我的问题是什么是使工作的原因实例3次(因为每次作业正在运行它的本身3次),如果停止作业178,为什么它再次运行退出数据作业ID:178

I want to make sure that only one job instance is allowed to run. and if another instance is already running then stop it.
So i implemented a listener that checks the number of running Jobs like below: (i'm not sur if it's the correct impl to stop the jobs)

public class SingleJobInstanceListener implements JobExecutionListener {

    @Override
    public void beforeJob(JobExecution jobExecution) {
        final String jobName = jobExecution.getJobInstance().getJobName();
        LOGGER.info("Listener to check one {} job instance is running", jobName);
        if (Constants.JOB_NAME.equals(jobName)) {
            final Set<JobExecution> executionSet = jobExplorer.findRunningJobExecutions(jobName);
            if (executionSet.size() > 1) {
                for (JobExecution execution : executionSet) {
                        execution.stop();
                        LOGGER.info("{} job instance {} is stopped", jobName, execution.getJobInstance().getInstanceId());
                }
            }
        }
    }
    
}

to run the job:

 @Scheduled(cron = "0/10 * * * * *")
 public void runSpringBatchJob() {
     LOGGER.info("Job was started");
     final JobExecution streamExecution = jobLauncher.run(job, newExecution());
     LOGGER.info("Exit data job with status: {}", streamExecution.getStatus());
     LOGGER.info("Exit data job ID: {}", streamExecution.getJobId());
     LOGGER.info("-----------------------------------------------------");
}

LOG:

SpringBatchJobLauncher       : Job was started
SingleJobInstanceListener    : Listener to check one testJob job instance is running
SingleJobInstanceListener    : testJob job instance 178 is stopped
SingleJobInstanceListener    : testJob job instance 160 is stopped
SingleJobInstanceListener    : testJob job instance 154 is stopped
StreamWriter                 : Writing data: [......]
SpringBatchJobLauncher       : Exit data job with status: COMPLETED
SpringBatchJobLauncher       : Exit data job ID: 178
SpringBatchJobLauncher       : -----------------------------------------------------
SpringBatchJobLauncher       : Job was started
SingleJobInstanceListener    : Listener to check one testJob job instance is running
SingleJobInstanceListener    : testJob job instance 160 is stopped
SingleJobInstanceListener    : testJob job instance 179 is stopped
SingleJobInstanceListener    : testJob job instance 154 is stopped
StreamWriter                 : Writing data: [......]
SpringBatchJobLauncher       : Exit data job with status: COMPLETED
SpringBatchJobLauncher       : Exit data job ID: 179

so my question is what is the reason that makes the job instanced 3 times (because each time the job is running it's instanced 3 times), and if the job 178 is stoped why is it running again Exit data job ID: 178

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2025-02-09 15:04:17

鉴于您正确地定义了一个集中的交易库存储库,并正确设计具有独特的识别作业参数的工作实例,因此您要实现的功能已经由Spring Batch提供。

使用此侦听器方法,您正在运行额外的作业执行,并检查当前是否有另一个工作。借助Spring Batch的内置功能,第二个作业执行甚至还没有开始,该作业启动器将阻止在启动时(这种失败快速的方法更有效)。

像您似乎拥有的本地计划的作业一样,一个很好的识别作业参数可能是运行时间戳。例如,您将每10分钟就有一个工作实例。现在,如果另一种计划的方法试图运行相同的作业实例,则与第一个方法同时说9:10 ,春季批次将阻止其中一个人运行,这要归功于集中的交易工作存储库方法。

这也可以在聚类的环境中起作用,在该环境中,您的应用程序的另一个实例(在JVM级别)试图与第一个应用程序同时运行9:10 的相同作业实例。春季批次还将防止同一实例的重复作业执行。但是,在不保证时钟同步的分布式环境中,单个时间戳参数不足以识别工作实例,您需要将另一个歧视器添加到设置的识别作业参数中,以便在集群中唯一地识别工作实例等级。

The feature you are trying to implement is already provided by Spring Batch, given that you correctly define a centralized transactional job repository and correctly design job instances with distinct identifying job parameters.

With this listener approach, you are running an additional job execution and checking if there is another one currently running. With Spring Batch's built-in feature, the second job execution is not even started, the job launcher will prevent that at startup time (this fail-fast approach is more efficient).

With locally scheduled jobs like you seem to have, a good identifying job parameter could be the run timestamp. For example, you will have a job instance every 10 minutes. Now if another scheduled method tries to run the same job instance say of 9:10 at the same time as the first method, then Spring Batch will prevent one of them from running, thanks to the centralized transactional job repository approach.

This also works in a clustered environment where another instance of your application (at the JVM level) tries to run the same job instance of 9:10 at the same time as the first application. Spring Batch will also prevent a duplicate job execution of the same instance. However, in a distributed environment where clock synchronization is not guaranteed, the single timestamp parameter won't be enough to identify job instances, you would need to add another discriminator to the identifying job parameters set in order to uniquely identify job instances at the cluster level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文