Table of Contents | Lesson 2 ›
Lesson 1: Using Quartz
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).
scheduler實例化之後才能被使用。你可以使用ScheduleFactory來實例化。你可以在JDNI store中維護一個Quartz的實例,也可以使用一種更爲簡便的方式,也就是直接使用工廠來創建實例(例子如下):
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.
當Scheduler實例化以後,就可以啓動(就緒狀態)和關閉它。注意,當scheduler被關閉以後,除非重新實例化,否則不能直接重啓。triggers在scheduler啓動之前不會工作,及時trigger處於暫停狀態(任務也不會執行)。
Here’s a quick snippet of code, that instantiates and starts a scheduler, and schedules a job for execution:
下面是實例化scheduler、啓動scheduler和編排任務的代碼片段:
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);
As you can see, working with quartz is rather simple. In Lesson 2 we’ll give a quick overview of Jobs and Triggers, and Quartz’s API so that you can more fully understand this example.
就和你看到的一樣,運行quartz非常簡單。在Lesson 2中我們會快速瀏覽Jobs、Triggers和Quartz的API來幫助更好的理解這個例子。