1.下载Quartz,我下载的是1.8.5,将quartz-all-1.8.5拷贝到web项目的lib目录中。除此之外还需要将以下
- jta-1.1
- log4j-1.2.14
- slf4j-api-1.6.1
- slf4j-nop-1.5.2
- slf4j-log4j12-1.6.1
- commons-beanutils-1.7.0.jar
- commons-collections-3.1.jar
- commons-logging-1.0.4.jar
拷贝到web项目的lib目录中,不然会启动tomcat会产生如下错误:
#缺少log4j-1.2.14,slf4j-api-1.6.1,slf4j-log4j12-1.6.1 java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory #缺少jta-1.1 javax/transaction/UserTransaction at java.lang.Class.getDeclaredMethods0(Native Method)
2.新建一个任务类,需要实现Quartz的Job接口。
package me.hujun.demo.quartz; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("Start MyJob at " + new Date()); } }
3.在src目录下新建quartz.properties,这个文件可以直接充quartz的jar包中拷贝。但需要加入两行新的内容,分别用来指定jobInitializer class和jobInitializer需要解析的配置文件路径(清单的最后两行配置)。
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName = TestScheduler org.quartz.scheduler.instanceId = AUTO #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 3 org.quartz.threadPool.threadPriority = 5 #============================================================================ # Configure JobStore #============================================================================ #org.quartz.jobStore.misfireThreshold = 60000 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #============================================================================ # Configure Plugins #============================================================================ #org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin #org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin #org.quartz.plugin.jobInitializer.fileNames = quartz_jobx.xml #org.quartz.plugin.jobInitializer.overWriteExistingJobs = true #org.quartz.plugin.jobInitializer.failOnFileNotFound = true #org.quartz.plugin.jobInitializer.scanInterval = 10 #org.quartz.plugin.jobInitializer.wrapInUserTransaction =false #org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin #如果这里不指定,默认会读取WEB-INFO/CLASSES目录下的quartz_jobs.xml文件,名称不能改变 #如果有多个job xml文件,使用","隔开 org.quartz.plugin.jobInitializer.fileNames = quartz_jobs.xml org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin org.quartz.plugin.shutdownhook.cleanShutdown =true4.编写quartz_job.xml(上述清单中org.quartz.plugin.jobInitializer.fileNames的值),配置job和trigger。
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<name>my_job</name>
<group>DEFAULT</group>
<description>
this is HelloWorld......
</description>
<job-class>org.hzy.jobs.My_Job</job-class>
</job>
<trigger>
<cron>
<name>my_job_trigger</name>
<job-name>my_job</job-name>
<cron-expression>0/10 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<name>my_job</name>
<group>DEFAULT</group>
<description>
this is HelloWorld......
</description>
<job-class>org.hzy.jobs.My_Job</job-class>
</job>
<trigger>
<cron>
<name>my_job_trigger</name>
<job-name>my_job</job-name>
<cron-expression>0/10 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
5.修改web.xml。
<servlet> <servlet-name>quartz</servlet-name> <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> <init-param> <param-name>config-file</param-name> <param-value>/quartz.properties</param-value> </init-param> <init-param> <param-name>shutdown-on-unload</param-name> <param-value>true</param-value> </init-param>
<init-param> <param-name>start-scheduler-on-load</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
或启用监听器QuartzInitializerListener
6.启动tomcat<context-param> <param-name>config-file</param-name> <param-value>/some/path/my_quartz.properties</param-value> </context-param> <context-param> <param-name>shutdown-on-unload</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>start-scheduler-on-load</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class> org.quartz.ee.servlet.QuartzInitializerListener </listener-class> </listener>