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

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

问题描述

如何检查Quartz cron作业是否正在运行?

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

感谢,
Kusuma

Thanks, Kusuma

推荐答案

scheduler.getCurrentlyExecutingJobs()在大多数情况下应该工作。但请记住不要在Job类中使用它,因为它使用ExecutingJobsManager(一个JobListener)将正在运行的作业放置到在作业类之前运行的HashMap,因此使用此方法检查作业是否正在运行将肯定返回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实例中执行的作业,而不是整个集群。如果在集群中运行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天全站免登陆