quartz2.2.1 web配置

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

將下載包中lib目錄下的全部jar包導入到項目中linux

由於quartz依賴log4j,須要編寫log4j.propertiesweb

log4j.propertiesspring

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接口express

HelloJob.javaapache

 

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文件tomcat

quartz.propertiesapp

 

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

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

相關文章
相關標籤/搜索