Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether Quartz cron job is running?

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

like image 918
kusumat Avatar asked Jun 18 '14 09:06

kusumat


People also ask

How do I run a Quartz job?

In the first step, we have to initialize the scheduler instance from Quartz by using StdSchedulerFactory() method. After that, we start the scheduler instance with the Quartz API start() method. Start the scheduler instance with Quartz API start()

Does Quartz use cron?

Quartz scheduler offers two kind of triggers, simple trigger and cron trigger. If the schedule is based on specified intervals then a simple trigger will do but if your job needs to be fired based on calendar-like notions then you need to use cron triggers.

How do you stop a job in quartz scheduler?

scheduler. deleteJob(jobKey(<JobKey>, <JobGroup>)); This method will only interrupt/stop the job uniquely identified by the Job Key and Group within the scheduler which may have many other jobs running.

What is Quartz cron expression?

quartz. Trigger. A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field.


1 Answers

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.

like image 137
arganzheng Avatar answered Oct 18 '22 00:10

arganzheng