如何判断Quartz作业是否已暂停

huangapple go评论55阅读模式
英文:

How to find out if the Quartz Job is paused

问题

在我的Spring Boot应用程序中,我使用了Quartz的org.quartz.Scheduler来调度一个任务:

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

然后我将其暂停:

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

如何获取此作业的状态以查看是否已暂停?

英文:

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?

答案1

得分: 1

pauseJob 方法的文档说明如下:
> 暂停具有给定键的 JobDetail - 通过暂停其所有当前触发器。

因此,您可以通过检查所有触发器是否已暂停来确定作业是否已暂停:

List<Trigger> triggers = scheduler.getTriggersOfJob(JobKey.jobKey("myJob"))

boolean paused = triggers
    .stream()
    .allMatch(trigger -> 
        scheduler.getTriggerState(trigger.key) == PAUSED 
    )
英文:

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 
    )

huangapple
  • 本文由 发表于 2020年8月18日 22:56:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63471326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定