http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-01.html
Before you can use the scheduler, it needs to be instantiated (who’d have guessed?). To do this, you use a SchedulerFactory. Some users of Quartz may keep an instance of a factory in a JNDI store, others may find it just as easy (or easier) to instantiate and use a factory instance directly (such as in the example below).
Once a scheduler is instantiated, it can be started, placed in stand-by mode, and shutdown. Note that once a scheduler is shutdown, it cannot be restarted without being re-instantiated. Triggers do not fire (jobs do not execute) until the scheduler has been started, nor while it is in the paused state.
Here’s a quick snippet of code, that instantiates and starts a scheduler, and schedules a job for execution:
在使用scheduler之前需要被实例化,可以通过实现ISchedulerFactory来做到这一点(To do this, you use an implementor of ISchedulerFactory.)。
scheduler被实例化后,它就可以被启动,处于等待模式(placed in stand-by mode), 被关闭。一旦scheduler被关闭,它不能再被重新启动,除非重新实例化它。只有scheduler 已经被启动或者不处于暂停状态,触发器才能被触发(任务能被执行)(Triggers do not fire (jobs do not execute) until the scheduler has been started, nor while it is in the paused state.)
下面是一个代码片段,实例化并且启动一个scheduler,同时安排执行一个job。
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("myJob", "group1")
.build();
// Trigger the job to run now, and then every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);