从下载连接取得Quartz的发布版本,并且解压到到本地 目录。这个下载文件包含了一个预先构建好的Quartz二进制文件(quartz.jar),你可以将它放进自己的应用中。Quartz框架只需要少数的 第三方库,并且这些三方库是必需的,你很可能已经在使用这些库了。
你要把Quartz的安装目录的
quartz.properties文件
Quartz有一个叫做 quartz.properties的配置文件,它允许你修改框架运行时环境。缺省是使用Quartz.jar里面的quartz.properties 文件。当然,你应该创建一个quartz.properties文件的副本并且把它放入你工程的classes目录中以便类装载器找到它。 quartz.properties样本文件如例1所示。
例1.quartz.properties文件允许修改Quartz运行环境:
一旦将Quartz.jar文件和第三方库加到自己的工程里面并且quartz.properties文件在工程的classes目录中,就可以创建作业了。
1. 在web.xml里面加上
<listener>
<listener-class>package.ContextListener</listener-class>
</listener>
这样就可以在部署的时候启动定时器。
package.ContextListener.java
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.SchedulerException;
public class ContextListener implements ServletContextListener {
Runner qRunner = null;
public void contextInitialized(ServletContextEvent arg0) {
// init job
// start schedule task
try {
qRunner = new Runner();
qRunner.task();
} catch (Exception e) {
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent arg0) {
try {
// stop task
qRunner.stop();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
/**
*/
public class Runner {
static Scheduler scheduler = null;
public void task() throws SchedulerException {
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
scheduler = schedulerFactory.getScheduler();
// current time
long ctime = System.currentTimeMillis();
// Initiate JobDetail with job name, job group, and executable job class
JobDetail jobDetail = new JobDetail("jobDetail1", "jobDetailGroup1",
YourScheduleJob.class);
// Initiate CronTrigger with its name and group name
CronTrigger cronTrigger = new CronTrigger("cronTrigger",
"triggerGroup1");
try {
// setup CronExpression
CronExpression cexp = new CronExpression("0/180 * * * * ?");
// Assign the CronExpression to CronTrigger
cronTrigger.setCronExpression(cexp);
} catch (Exception e) {
e.printStackTrace();
}
// schedule a job with JobDetail and Trigger
scheduler.scheduleJob(jobDetail, cronTrigger);
// start the scheduler
scheduler.start();
}
public void stop() throws SchedulerException {
scheduler.shutdown();
}
public static void main(String args[]) {
try {
Runner qRunner = new Runner();
qRunner.task();
} catch (Exception e) {
e.printStackTrace();
}
}
}
YourScheduleJob.java
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class YourScheduleJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("In SimpleQuartzJob - executing its JOB at "
+ new Date() + " by " + context.getTrigger().getName());
// your job here
}
}