Quartz - 基于作业变量的关机调度程序

Quartz - Shutdown Scheduler based on Job Variable

我正在使用 Quartz 在 Java 中安排工作。 调度程序无限期地重复。 如果在 Job.class 中发生某个异常,我需要关闭调度程序。 如何让作业通知调度 class 到 shutdown()?

谢谢, 雷克

Quartz 的工作是 运行 通过调用它们的 execute(JobExecutionContext) 方法。这个 JobExecutionContext 对象有一个 getScheduler() 方法来访问正在 运行 完成你的工作的 Scheduler

所以你可以这样做:

class YourJob implements Job {
    //...
    @Override
    public void execute(final JobExecutionContext context) throws JobExecutionException {
        //...
        if (someCondition) {
            try {
                context.getScheduler().shutdown();
            } catch (SchedulerException e) {
                // log or rethrow!
            }
        }
        //...

但是,我不太确定在 运行 执行作业的过程中关闭调度程序是否是个好主意。你为什么要那样做?如果你想做的只是再次停止 运行ning 的一些工作,那么只 unschedule 可能会更好:

Unscheduling a Particular Trigger of Job:

// Unschedule a particular trigger from the job (a job may have more than one trigger)
scheduler.unscheduleJob(triggerKey("trigger1", "group1"));

Deleting a Job and Unscheduling All of Its Triggers

scheduler.deleteJob(jobKey("job1", "group1"));