quartz定时器的使用

菜鸟的脚步 2013-03-23

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/UserTransactionatjava.lang.Class.getDeclaredMethods0(NativeMethod)

2.新建一个任务类,需要实现Quartz的Job接口。

packageme.hujun.demo.quartz;

importjava.util.Date;

importorg.quartz.Job;

importorg.quartz.JobExecutionContext;

importorg.quartz.JobExecutionException;

publicclassMyJobimplementsJob{

@Override

publicvoidexecute(JobExecutionContextarg0)throwsJobExecutionException{

System.out.println("StartMyJobat"+newDate());

}

}

3.在src目录下新建quartz.properties,这个文件可以直接充quartz的jar包中拷贝。但需要加入两行新的内容,分别用来指定jobInitializerclass和jobInitializer需要解析的配置文件路径(清单的最后两行配置)。

#============================================================================

#ConfigureMainSchedulerProperties

#============================================================================

org.quartz.scheduler.instanceName=TestScheduler

org.quartz.scheduler.instanceId=AUTO

#============================================================================

#ConfigureThreadPool

#============================================================================

org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool

org.quartz.threadPool.threadCount=3

org.quartz.threadPool.threadPriority=5

#============================================================================

#ConfigureJobStore

#============================================================================

#org.quartz.jobStore.misfireThreshold=60000

org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore

#============================================================================

#ConfigurePlugins

#============================================================================

#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

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=true

4.编写quartz_job.xml(上述清单中org.quartz.plugin.jobInitializer.fileNames的值),配置job和trigger。

<?xmlversion="1.0"encoding="UTF-8"?>

<job-scheduling-dataxmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingDatahttp://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"

version="1.8">

<schedule>

<job>

<name>my_job</name>

<group>DEFAULT</group>

<description>

thisisHelloWorld......

</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

<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>

6.启动tomcat

相关推荐