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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24282441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:17:37  来源:igfitidea点击:

How to check whether Quartz cron job is running?

javacronquartz-schedulerschedulerjob-scheduling

提问by kusumat

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

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

采纳答案by arganzheng

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:

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

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;
}

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.

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

回答by rafaborrego

Have you looked at this answer? Try with:

你看过这个答案吗?尝试:

scheduler.getCurrentlyExecutingJobs()

scheduler.getCurrentlyExecutingJobs()

回答by ccu

If you notice in the QUARTZ_TRIGGERS table, there is a TRIGGER_STATE column. This tells you the state of the trigger (TriggerState) for a particular job. In all likelihood your app doesn't have a direct interface to this table but the quartz scheduler does and you can check the state this way:

如果您注意到 QUARTZ_TRIGGERS 表中有一个 TRIGGER_STATE 列。这会告诉您特定作业的触发器 ( TriggerState)状态。很可能您的应用程序没有与该表的直接接口,但石英调度程序有,您可以通过以下方式检查状态:

private Boolean isJobPaused(String jobName) throws SchedulerException {

    JobKey jobKey = new JobKey(jobName);
    JobDetail jobDetail = scheduler.getJobDetail(jobKey);
    List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobDetail.getKey());
    for (Trigger trigger : triggers) {
        TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
        if (TriggerState.PAUSED.equals(triggerState)) {
            return true;
        }
    }
    return false;
}