Search code examples
javaquartz

How to find out if the Quartz Job is paused


In my Spring Boot application I used Quartz org.quartz.Scheduler to schedule a job:

final JobDetail job = JobBuilder.newJob().ofType(ReportSendingJob.class)
    .storeDurably()
    .withIdentity("myJob")
    .build();

final CronTrigger trigger = TriggerBuilder.newTrigger().forJob(job)
    .withIdentity(scheduleName)
                .withSchedule(CronScheduleBuilder.cronSchedule(schedule.getCronExpression()).withMisfireHandlingInstructionDoNothing())
                .build();

scheduler.scheduleJob(job, trigger);

then I paused it:

scheduler.pauseJob(new JobKey("myJob"));

How can I get the status of this job to see if it is paused or not?


Solution

  • Documentation of pauseJob method says

    Pause the JobDetail with the given key - by pausing all of its current Triggers.

    So you could check if job is paused by checking if all triggers are paused:

    List<Trigger> triggers = scheduler.getTriggersOfJob(JobKey.jobKey("myJob"))
    
    boolean paused = triggers
        .stream()
        .allMatch( trigger -> 
            scheduler.getTriggerState(trigger.key) == PAUSED 
        )