white knight

导航

 

首先到http://www.quartz-scheduler.org/downloads/catalog下载工具包

将下载包中lib目录下的所有jar包导入到项目中

因为quartz依赖log4j,需要编写log4j.properties

log4j.properties

log4j.rootCategory=INFO, stdout , R     
     
log4j.appender.stdout=org.apache.log4j.ConsoleAppender     
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout     
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n     
      
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender     
log4j.appender.R.File=D:\\Tomcat 5.5\\logs\\qc.log     
log4j.appender.R.layout=org.apache.log4j.PatternLayout     
1log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n     
     
log4j.logger.com.neusoft=DEBUG     
log4j.logger.com.opensymphony.oscache=ERROR     
log4j.logger.net.sf.navigator=ERROR     
log4j.logger.org.apache.commons=ERROR     
log4j.logger.org.apache.struts=WARN     
log4j.logger.org.displaytag=ERROR     
log4j.logger.org.springframework=DEBUG     
log4j.logger.com.ibatis.db=WARN     
log4j.logger.org.apache.velocity=FATAL     
     
log4j.logger.com.canoo.webtest=WARN     
     
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN     
log4j.logger.org.hibernate=DEBUG     
log4j.logger.org.logicalcobwebs=WARN    
  
log4j.rootCategory=INFO, stdout , R  
  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n  
   
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender  
log4j.appender.R.File=D:\\Tomcat 5.5\\logs\\qc.log  
log4j.appender.R.layout=org.apache.log4j.PatternLayout  
1log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n  
  
log4j.logger.com.neusoft=DEBUG  
log4j.logger.com.opensymphony.oscache=ERROR  
log4j.logger.net.sf.navigator=ERROR  
log4j.logger.org.apache.commons=ERROR  
log4j.logger.org.apache.struts=WARN  
log4j.logger.org.displaytag=ERROR  
log4j.logger.org.springframework=DEBUG  
log4j.logger.com.ibatis.db=WARN  
log4j.logger.org.apache.velocity=FATAL  
  
log4j.logger.com.canoo.webtest=WARN  
  
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN  
log4j.logger.org.hibernate=DEBUG  
log4j.logger.org.logicalcobwebs=WARN  

 

然后编写具体的任务,需要实现Job接口

HelloJob.java

 

package test;

import
org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { @Override public void execute(JobExecutionContext jec) throws JobExecutionException { System.out.println("hello world"); } }

 

 

编写quartz.properties文件

quartz.properties

 

org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: 60000

org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = jobs.xml #注意这里
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

 

 

最后编写jobs.xml

jobs.xml

<?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-very-clever-job</name>
            <group>MYJOB_GROUP</group>
            <description>The job description</description>
            <job-class>test.HelloJob</job-class>
        </job>
        <trigger>
            <cron>
                <name>my-trigger</name>
                <group>MYTRIGGER_GROUP</group>
                <job-name>my-very-clever-job</job-name>
                <job-group>MYJOB_GROUP</job-group>
                <cron-expression>0 0/1 * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>

说明一下这个jobs.xml,<job></job>标签中要填写的是定时任务,就是实现Job接口的类,例如上面的HelloJob.java

<trigger></trigger>代表触发器,<corn-expression></corn-expression>表示触发时间,现在配置的是每隔一分钟执行一遍任务,具体配置类似于linux下的定时任务

 

最后在web.xml中加上以下代码:

<context-param>
         <param-name>quartz:config-file</param-name>
         <param-value>/quartz.properties</param-value>
     </context-param>
     <context-param>
         <param-name>quartz:shutdown-on-unload</param-name>
         <param-value>true</param-value>
     </context-param>
     <context-param>
         <param-name>quartz:wait-on-shutdown</param-name>
         <param-value>true</param-value>
     </context-param>
     <context-param>
         <param-name>quartz:start-scheduler-on-load</param-name>
         <param-value>true</param-value>
     </context-param>
     
     <servlet> 
        <servlet-name>QuartzInitializer</servlet-name> 
        <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> 
        <init-param>
            <param-name>shutdown-on-unload</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

 

tomcat7

 

 

posted on 2013-11-11 10:35  white knight  阅读(3191)  评论(1编辑  收藏  举报