クォーツスケジューラの新機能。私が達成しようとしているのは、将来 1 つのトリガーを起動してから、スケジューラーをシャットダウンすることです。これに使用scheduler.shutdown(true)
していますが、ジョブを実行する前にシャットダウンします。Web アプリにスケジューラを実装するため、shutdown() を呼び出す必要があります。
では、ジョブの実行後にスケジューラをシャットダウンするにはどうすればよいですか?
仕事:
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