1、下载quartz的相应版本(本机是quartz-1.6.5),解压后将quartz-1.6.5.jar以及lib中各个子目录中的jar包加入到工程的lib中,其中有些jar不是必须的,取决于你要使用的功能。
2、编写quartz.properties 文件。
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName = QuartzScheduler org.quartz.scheduler.instanceId = AUTO #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 5 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 = jobs.xml org.quartz.plugin.jobInitializer.overWriteExistingJobs = true org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.jobInitializer.scanInterval = 10 org.quartz.plugin.jobInitializer.wrapInUserTransaction = false3、编写jobs.xml文件.
<?xml version='1.0' encoding='utf-8'?> <quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd" version="1.5"> <job> <job-detail> <name>HelloWorld</name> <group>DEFAULT</group> <description> A job that just for test </description> <job-class> com.stone.quartz.HelloWorld </job-class> <!-- 易变,活泼 --> <volatility>false</volatility> <!-- 耐久性 --> <durability>false</durability> <!-- 恢复性 --> <recover>false</recover> </job-detail> <trigger> <simple> <name>HelloTrigger1</name> <group>DEFAULT</group> <job-name>HelloWorld</job-name> <job-group>DEFAULT</job-group> <start-time>2011-08-30T07:22:36</start-time> <!--重复次数 --> <repeat-count>-1</repeat-count> <!-- 重复时间间隔 --> <repeat-interval>10000</repeat-interval> </simple> </trigger> </job> </quartz>
4、注意以上两个文件的位置,应该位于工程的src目录下,如果是发布好的工程对应在/WEB-INFO/classes下面。
5、修改web.xml增加servlet。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>QuartzInitializer</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> <load-on-startup>1</load-on-startup> </servlet> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
package com.stone.quartz; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloWorld implements Job { private static Log logger = LogFactory.getLog(HelloWorld.class);// 日志记录器 public void execute(JobExecutionContext context) throws JobExecutionException { logger.info("Hello World! - " + new Date()); } }