How to check whether Quartz cron job is running?

放肆的年华 提交于 2019-11-27 02:43:35

问题


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


回答1:


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

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.




回答2:


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:

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



回答3:


Have you looked at this answer? Try with:

scheduler.getCurrentlyExecutingJobs()



来源:https://stackoverflow.com/questions/24282441/how-to-check-whether-quartz-cron-job-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!