Quartz2-Quartz与tomcat集成

Quartz2-Quartz与tomcat集成

  Quartz只与tomcat简单集成,不用其他框架。

  • 写一个要需要执行任务的类DumbJob,实现Job接口
Java代码  收藏代码
  1. package com.lll;  
  2.   
  3. import org.quartz.Job;  
  4. import org.quartz.JobExecutionContext;  
  5. import org.quartz.JobExecutionException;  
  6.   
  7. public class DumbJob implements Job {  
  8.   
  9.     public void execute(JobExecutionContext arg0) throws JobExecutionException {  
  10.         System.err.println("DumbJob is executing.");  
  11.     }  
  12.       
  13. }  

 

  • 建两个配置文件quartz.properties和quartz_job.xml,放在classes下
  • quartz.properties
Java代码  收藏代码
  1. org.quartz.scheduler.instanceName = TestScheduler  
  2. org.quartz.scheduler.instanceId = one  
  3.   
  4. org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
  5. org.quartz.threadPool.threadCount = 2  
  6. org.quartz.threadPool.threadPriority = 4  
  7.   
  8. org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin  
  9. org.quartz.plugin.triggHistory.triggerFiredMessage = Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}  
  10. org.quartz.plugin.triggHistory.triggerCompleteMessage = Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}  
  11.   
  12. org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin  
  13. org.quartz.plugin.jobInitializer.fileName = quartz_job.xml  
  14. org.quartz.plugin.jobInitializer.overWriteExistingJobs = false  
  15. org.quartz.plugin.jobInitializer.failOnFileNotFound = true  
  16. org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin  
  17. org.quartz.plugin.shutdownhook.cleanShutdown = true  

 

  • quartz_job.xml
Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <quartz>   
  3. <job>  
  4.     <job-detail>  
  5.       <name>helloworld</name>  
  6.       <group>group1</group>  
  7.       <job-class>com.lll.DumbJob</job-class>  
  8.     </job-detail>  
  9.     <trigger>  
  10.       <cron>  
  11.         <name>test</name>  
  12.         <group>group1</group>  
  13.         <job-name>helloworld</job-name>  
  14.         <job-group>group1</job-group>  
  15.         <cron-expression>0/1 * * * * ?</cron-expression>  
  16.      </cron>  
  17.     </trigger>  
  18. </job>  
  19. </quartz>  

 

 

  • 配置web.xml
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.       
  8.     <servlet>  
  9.          <servlet-name>  
  10.              QuartzInitializer  
  11.          </servlet-name>  
  12.          <servlet-class>  
  13.              org.quartz.ee.servlet.QuartzInitializerServlet  
  14.          </servlet-class>  
  15.          <init-param>  
  16.              <param-name>config-file</param-name>  
  17.              <param-value>/quartz.properties</param-value>  
  18.          </init-param>  
  19.          <init-param>  
  20.              <param-name>shutdown-on-unload</param-name>  
  21.              <param-value>true</param-value>  
  22.          </init-param>  
  23.          <load-on-startup>1</load-on-startup>  
  24.      </servlet>  
  25.       
  26.       
  27.       
  28.   <welcome-file-list>  
  29.     <welcome-file>index.jsp</welcome-file>  
  30.   </welcome-file-list>  
  31. </web-app>  

转自:http://stranger2008.iteye.com/blog/852258

posted @ 2015-09-22 11:13  harry_ma  阅读(1052)  评论(0编辑  收藏  举报
 aa
    部分内容来自网络,如有异议,请联系我.     


      成绩来自坚持, 灵感来自激情。

                                                               感谢父母和亲朋好友的爱与支持!