石英调度程序的新手。我想要实现的是在未来触发一个触发器,然后关闭调度程序。我正在使用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