如何检查 Quartz cron 作业是否正在运行? [英] How to check whether Quartz cron job is running?

查看:96
本文介绍了如何检查 Quartz cron 作业是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查预定的 Quartz cron 作业是否正在运行?是否有任何 API 可以进行检查?

How to check if scheduled Quartz cron job is running or not? Is there any API to do the checking?

推荐答案

scheduler.getCurrentlyExecutingJobs() 在大多数情况下应该可以工作.但是切记不要在Job类中使用它,因为它使用ExecutingJobsManager(a JobListener)将正在运行的Job放到一个HashMap中,HashMap在Job类之前运行,所以用这个方法检查Job是否正在运行肯定会返回true.一种简单的方法是检查触发时间是否不同:

scheduler.getCurrentlyExecutingJobs() should work in most case. But remember not to use it in Job class, for it use ExecutingJobsManager(a JobListener) to put the running job to a HashMap, which run before the job class, so use this method to check job is running will definitely return true. One simple approach is to check that fire times are different:

public static boolean isJobRunning(JobExecutionContext ctx, String jobName, String groupName)
        throws SchedulerException {
    List<JobExecutionContext> currentJobs = ctx.getScheduler().getCurrentlyExecutingJobs();

    for (JobExecutionContext jobCtx : currentJobs) {
        String thisJobName = jobCtx.getJobDetail().getKey().getName();
        String thisGroupName = jobCtx.getJobDetail().getKey().getGroup();
        if (jobName.equalsIgnoreCase(thisJobName) && groupName.equalsIgnoreCase(thisGroupName)
                && !jobCtx.getFireTime().equals(ctx.getFireTime())) {
            return true;
        }
    }
    return false;
}

另请注意,此方法不是集群感知的.也就是说,它只会返回当前在这个 Scheduler 实例中执行的 Jobs,而不是整个集群.如果在集群中运行 Quartz,它将无法正常工作.

Also notice that this method is not cluster aware. That is, it will only return Jobs currently executing in this Scheduler instance, not across the entire cluster. If you run Quartz in a cluster, it will not work properly.

这篇关于如何检查 Quartz cron 作业是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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