4

石英调度程序的新手。我想要实现的是在未来触发一个触发器,然后关闭调度程序。我正在使用scheduler.shutdown(true)它,但它在执行作业之前关闭。我必须调用 shutdown(),因为我要在 Web 应用程序中实现调度程序。

那么如何在作业执行后关闭调度程序?

工作:

public class HelloJob implements Job {

    public HelloJob(){
    }

    public void execute(JobExecutionContext context)
            throws JobExecutionException {
                System.out.println("Hello Quartz on " + new Date());    
            }
}

调度器:

public class QuartzTest {

    public void scheduleLoad(String time) {
        try {
            // Transform user input into a date
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy:HH:mm:ss");
            Date scheduleDate = dateFormat.parse(time);

            // Print Current vs. Scheduled time/date
            System.out.println("Current time - " + new Date());
            System.out.println("Scheduled time - " + scheduleDate); 

            // Grab the Scheduler instance from the Factory 
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            // and start it off
            scheduler.start();

            // Define a job and tie it to a class
            JobDetail job = newJob(HelloJob.class)
                    .withIdentity("job1", "group1")
                    .build();

            // Trigger job to run now and repeat every 10 secs
            SimpleTrigger trigger = (SimpleTrigger) newTrigger()
                    .withIdentity("trigger1", "group1")
                    .startAt(scheduleDate)
                    .forJob("job1","group1")
                    .build();

            // Schedule job using trigger
            scheduler.scheduleJob(job, trigger);

            // Shutdown the scheduler after job is executed
            scheduler.shutdown(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String runTime = "04/10/2013:20:07:00";
        QuartzTest quartz = new QuartzTest();
        quartz.scheduleLoad(runTime);
    }
}

输出:

Current time - Wed Apr 10 20:06:31 IST 2013
Scheduled time - Wed Apr 10 20:07:00 IST 2013
[main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
[main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
[main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
[main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.1.7 created.
[main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
[main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.1.7) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

[main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
[main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.1.7
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.

石英属性:

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.scheduler.skipUpdateCheck: true
4

1 回答 1

6

我认为您误解了scheduler.shutdown(true); 它将等待执行作业完成的目的,但它不会等待计划的作业开始和完成。在您关闭调度程序之前,您的工作没有开始。Thread.sleep(wait); 你可以在关闭它之前放一个 。要让您的代码按照我理解的方式运行,请删除此行(您基本上不需要关闭调度程序):

 // Shutdown the scheduler after job is executed
 scheduler.shutdown(true);

此外,移动此行使其仅执行一次。你把它放在哪里,取决于你的应用程序,它可以在独立应用程序的 main 方法中,或者如果在 Web 应用程序中运行,它可以在 Servlet 或 Listener 的 init 方法中:

 // and start it off
 scheduler.start();

跟进:

我不明白你为什么需要关闭 Quartz。如果另一个用户需要安排任务会发生什么?您是否计划为每个计划的作业启动一个不同的 Quartz 实例?让它运行并根据需要安排任务会更有意义。这是运行 Quartz 的正常方式。您也许可以让它以您想要的方式运行,但它可能是人为的。如果您真的只是想要在任务运行后启动和关闭的东西,您可能需要查看 JDK 提供的 Timer 和 TimerTask。请参见此处的示例。

于 2013-04-10T15:43:10.950 回答