现在的位置: 首页 > 综合 > 正文

Quartz 任务调度 0 0 17 * * ?

2017年11月20日 ⁄ 综合 ⁄ 共 7238字 ⁄ 字号 评论关闭

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

工程目录结构

 

 

 

spring + quartz JAR包

 

 

 

ApplicationContext.xml(beans.xml)

  1. <? xml version = "1.0" encoding = "UTF-8" ?>   
  2.   
  3. < beans xmlns = "http://www.springframework.org/schema/beans"   
  4.   
  5.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  6.   
  7.     xmlns:context = "http://www.springframework.org/schema/context"  
  8.   
  9.     xmlns:aop = "http://www.springframework.org/schema/aop"   
  10.   
  11.     xsi:schemaLocation = "http://www.springframework.org/schema/beans  
  12.   
  13.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  14.   
  15.            http://www.springframework.org/schema/context   
  16.   
  17.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  18.   
  19.            http://www.springframework.org/schema/aop   
  20.   
  21.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >  
  22.   
  23.     
  24.   
  25.     < context:annotation-config />   
  26.   
  27.     < context:component-scan base-package = "com" />   
  28.   
  29.     < aop:aspectj-autoproxy />   
  30.   
  31.     
  32.   
  33.     <!-- 要调用的工作类 -->   
  34.   
  35.     < bean id = "quartzJob" class = "com.kay.quartz.QuartzJob" ></ bean >  
  36.   
  37. <!-- 定义调用对象和调用对象的方法 -->   
  38.   
  39.     < bean id = "jobtask" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >  
  40.   
  41.         <!-- 调用的类 -->   
  42.   
  43.         < property name = "targetObject" >   
  44.   
  45.             < ref bean = "quartzJob" />   
  46.   
  47.         </ property >   
  48.   
  49.         <!-- 调用类中的方法 -->   
  50.   
  51.         < property name = "targetMethod" >   
  52.   
  53.             < value > work </ value >   
  54.   
  55.         </ property >   
  56.   
  57.     </ bean >   
  58.   
  59.       
  60.   
  61.     <!-- 定义触发时间 -->   
  62.   
  63.     < bean id = "doTime" class = "org.springframework.scheduling.quartz.CronTriggerBean" >  
  64.   
  65.         < property name = "jobDetail" >   
  66.   
  67.             < ref bean = "jobtask" />   
  68.   
  69.         </ property >   
  70.   
  71.         <!-- cron 表达式 -->   
  72.   
  73.         < property name = "cronExpression" >   
  74.   
  75.             < value > 0 0 17 * * ? </ value >   
  76.   
  77.         </ property >   
  78.   
  79.     </ bean >   
  80.   
  81. <!-- 总管理类 如果将 lazy-init='false' 那么容器启动就会执行调度程序 -->   
  82.   
  83.     < bean id = "startQuertz" lazy-init = "false" autowire = "no" class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" >  
  84.   
  85.         < property name = "triggers" >   
  86.   
  87.             < list >   
  88.   
  89.                 < ref bean = "doTime" />   
  90.   
  91.             </ list >   
  92.   
  93.         </ property >   
  94.   
  95.     </ bean >   
  96.   
  97.     
  98.   
  99. </ beans >   



 

任务调度类

  1. package com.kay.quartz;   
  2.   
  3.     
  4.   
  5. import java.io.File;   
  6.   
  7. import java.io.FileFilter;   
  8.   
  9. import java.io.FileInputStream;   
  10.   
  11. import java.io.FileOutputStream;   
  12.   
  13. import java.util.Date;   
  14.   
  15.     
  16.   
  17. /**  
  18.  
  19.   * quartz 任务调度 , 每天下午 17:00, 自动将 e:/ 下的 jpg 图片 , 拷贝到 e:/aa/ 下  
  20.  
  21.   *  
  22.  
  23.   * 秒    分    时    月中天    月     周中天    [ 年 ]  
  24.  
  25.   *  
  26.  
  27.   * 0     0     17    *        *      ?  
  28.  
  29.   *  
  30.  
  31.   * @author leiwei 2011 - 11 - 29  
  32.  
  33.   *  
  34.  
  35.   *  
  36.  
  37.   */   
  38.   
  39. public class QuartzJob {   
  40.   
  41.     
  42.   
  43.     public void work() throws Exception {  
  44.   
  45.         int b = 0;   
  46.   
  47.     
  48.   
  49.         // Make sure the directory exists    
  50.   
  51.         File dir = new File( "e:/" );   
  52.   
  53.     
  54.   
  55.         if (!dir.exists()){   
  56.   
  57.             throw new Exception( "Directory not configured" );   
  58.   
  59.         }   
  60.   
  61.     
  62.   
  63.         // Use FileFilter to get only jpg files         
  64.   
  65.         FileFilter filter = new FileExtensionFileFilter( ".jpg" );    
  66.   
  67.         File[] files = dir.listFiles(filter);    
  68.   
  69.     
  70.   
  71.         // Return since there were no files   
  72.   
  73.         if (files == null || files. length <= 0) {        
  74.   
  75.             System. out .println( "No XML files found in " + dir);        
  76.   
  77.             return ;                
  78.   
  79.         }    
  80.   
  81.     
  82.   
  83.         // The number of jpg files         
  84.   
  85.         int size = files. length ;      
  86.   
  87.     
  88.   
  89.         // Iterate through the files found          
  90.   
  91.         for ( int i = 0; i < size; i++) {        
  92.   
  93.             File file = files[i];         
  94.   
  95.     
  96.   
  97.             // Log something interesting about each file.         
  98.   
  99.             File aFile = file.getAbsoluteFile();      
  100.   
  101.     
  102.   
  103.             FileInputStream ino = new FileInputStream(aFile);  
  104.   
  105.             FileOutputStream fos = new FileOutputStream( "e:/aa/" +file.getName());  
  106.   
  107.     
  108.   
  109.             while ((b=ino.read())>-1){   
  110.   
  111.                 fos.write(b);   
  112.   
  113.             }     
  114.   
  115.         }         
  116.   
  117.     
  118.   
  119.     }   
  120.   
  121. }   
  122.   
  123. // 内部类,过滤文件   
  124.   
  125. class FileExtensionFileFilter implementsFileFilter{  
  126.   
  127.     
  128.   
  129.     private   String extension ;   
  130.   
  131.     
  132.   
  133.     public FileExtensionFileFilter(String extension) {  
  134.   
  135.         this . extension = extension;   
  136.   
  137.     }   
  138.   
  139.     
  140.   
  141.     public boolean accept(File file) {   
  142.   
  143.         String lCaseFilename = file.getName().toLowerCase();         
  144.   
  145.         return   (file.isFile() && (lCaseFilename.indexOf( extension ) >  0 )) ? true:false ;        
  146.   
  147.     }   
  148.   
  149.     
  150.   
  151. }   

 

 

测试

 

  1. package com.test;   
  2.   
  3.     
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6.   
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.   
  9.     
  10.   
  11. public class Test {   
  12.   
  13.     
  14.   
  15.     public static void main(String[] args) {   
  16.   
  17.         System. out .println( " 调度开始 ..." );   
  18.   
  19.         ApplicationContext apc = new ClassPathXmlApplicationContext( "beans.xml" );  
  20.   
  21.         System. out .println( " 调度结束 ..." );   
  22.   
  23.     }   
  24.   
  25. }   

 

参考: 

http://it.oyksoft.com/post/698/

 

续:上面只是quartz的独立应用,如何将它整合到实际的项目应用中了。
    在原有的ssh基础上,加入quartz.xml(在这里面配置所需的任务调度),
    再在web.xml文件contextConfigLocation下param-value中追加classpath*:quartz.xml
    结构图、文件如下

 

 quartz.xml

  1. <? xml version = "1.0" encoding = "UTF-8" ?>   
  2. < beans   
  3.     xmlns = "http://www.springframework.org/schema/beans"   
  4.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >   
  6.       
  7.     <!-- 对 testService 中的 testQuartz 方法进行定时任务调度 -->   
  8.     < bean id = "methodInvokingJobDetailForSms" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >          
  9.         < property name = "targetObject" >   
  10.             < ref bean = "testService" />   
  11.         </ property >           
  12.         < property name = "targetMethod" >   
  13.             < value > testQuartz </ value >   
  14.         </ property >     
  15.     </ bean >   
  16.     < bean id = "cronTriggerForSms" class = "org.springframework.scheduling.quartz.CronTriggerBean" >           
  17.        < property name = "jobDetail" >               
  18.             < ref bean = "methodInvokingJobDetailForSms" />           
  19.        </ property >           
  20.        < property name = "cronExpression" >               
  21.             < value > 0 * 12 * * ? </ value >        
  22.        </ property >       
  23.     </ bean >   
  24.     < bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" >           
  25.        < property name = "triggers" >               
  26.             < list >< ref local = "cronTriggerForSms" /></ list >           
  27.        </ property >       
  28.     </ bean >   
  29. </ beans >   

 

web.xml

  1. < context-param >   
  2.     < param-name > contextConfigLocation </ param-name >   
  3.     <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->   
  4.     < param-value >   
  5.         classpath*:beans.xml   
  6.         classpath*:quartz.xml   
  7.     </ param-value >   
  8. </ context-param >   

 

抱歉!评论已关闭.