zoukankan      html  css  js  c++  java
  • 项目内添加quartz定时任务

    该功能是在原来的test项目上做的,仅仅只是自己做测试用的

    1、添加了quartz包和quartz相关的配置文件

    2、添加依赖的jar包

     1  <dependency>
     2               <groupId>org.quartz-scheduler</groupId>
     3               <artifactId>quartz</artifactId>
     4               <version>2.2.1</version>
     5           </dependency>
     6       <dependency>
     7           <groupId>org.quartz-scheduler</groupId>
     8           <artifactId>quartz-jobs</artifactId>
     9           <version>2.2.1</version>
    10       </dependency> 
    同时需要这个jar包的支持:<groupId>org.springframework</groupId>
                 <artifactId>spring-context-support</artifactId>

    3、job类

     1 package com.fh.test.quartz;
     2 
     3 
     4 import org.slf4j.Logger;
     5 import org.slf4j.LoggerFactory;
     6 
     7 public class MyJob {
     8     
     9     private final static Logger logger=LoggerFactory.getLogger(MyJob.class);
    10     public void execute(){
    11     
    12         logger.debug("
     job is start !!!");
    13         
    14         try {
    15             Thread.sleep(10000L);
    16         } catch (InterruptedException e) {
    17             e.printStackTrace();
    18         }finally{
    19             logger.debug("
     job is end !!!");
    20         }
    21     }
    22 
    23 }

    spring配置文件
      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <beans xmlns="http://www.springframework.org/schema/beans"
      3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
      4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
      5     xmlns:context="http://www.springframework.org/schema/context"
      6     xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jaxws="http://cxf.apache.org/jaxws"
      7     xmlns:http-conf = "http://cxf.apache.org/transports/http/configuration"
      8     xsi:schemaLocation="
      9     http://www.springframework.org/schema/beans
     10     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     11     http://www.springframework.org/schema/context 
     12     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     13     http://www.springframework.org/schema/tx 
     14     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
     15     http://www.springframework.org/schema/aop 
     16     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     17     http://www.springframework.org/schema/cache
     18     http://www.springframework.org/schema/cache/spring-cache.xsd
     19     http://cxf.apache.org/jaxws   
     20     http://cxf.apache.org/schemas/jaxws.xsd  
     21     http://cxf.apache.org/transports/http/configuration      
     22     http://cxf.apache.org/schemas/configuration/http-conf.xsd 
     23     ">
     24 
     25     <!-- job工作 -->
     26     <bean id="job" class="com.fh.test.quartz.MyJob"></bean>
     27     
     28    <!--  工作描述 -->
     29    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     30          <property name="targetObject" ref="job"></property>
     31          <property name="targetMethod" value="execute"></property>
     32    </bean>
     33     
     34     <!-- 触发器 -->
     35     <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
     36         <property name="jobDetail" ref="jobDetail"></property>
     37         <property name="cronExpression" value="* * * * * ? *"></property>
     38     </bean>
     39     
     40     <!-- 调度工厂 -->
     41     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     42         <property name="triggers">
     43            <list>
     44               <ref bean="cronTrigger"/>
     45            </list>
     46         </property>
     47     </bean>
     48     
     49     
     50     <!--
     51 字段                    允许值                        允许的特殊字符   
     52 秒                            0-59            , - * /   
     53 分                            0-59            , - * /   
     54 小时                       0-23            , - * /   
     55 日期                      1-31            , - * ? / L W C   
     56 月份           1-12 或者 JAN-DEC    , - * /   
     57 星期           1-7 或者 SUN-SAT     , - * ? / L C #   
     58 年(可选) 留空, 1970-2099   , - * /
     59 
     60 “*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。 
     61 “?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。 
     62 月份中的日期和星期中的日期这两个元素时互斥的一起应该通过设置一个问号(?)来表明不想设置那个字段。
     63 
     64 “-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
     65 
     66 “,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”。
     67 
     68 “/”字符用于指定增量。如:“0/15”在秒域意思是没分钟的0,15,30和45秒。“5/15”在分钟域表示没小时的5,20,35和50。符号“*”在“/”前面(如:*/10)等价于0在“/”前面(如:0/10)。记住一条本质:表达式的每个数值域都是一个有最大值和最小值的集合,如:秒域和分钟域的集合是0-59,日期域是1-31,月份域是1-12。字符“/”可以帮助你在每个字符域中取相应的数值。如:“7/6”在月份域的时候只有当7月的时候才会触发,并不是表示每个6月。
     69 
     70 L是‘last’的省略写法可以表示day-of-month和day-of-week域,但在两个字段中的意思不同,例如day-of-month域中表示一个月的最后一天。如果在day-of-week域表示‘7’或者‘SAT’,如果在day-of-week域中前面加上数字,它表示一个月的最后几天,例如‘6L’就表示一个月的最后一个星期五。
     71 
     72 字符“W”只允许日期域出现。这个字符用于指定日期的最近工作日。例如:如果你在日期域中写 “15W”,表示:这个月15号最近的工作日。所以,如果15号是周六,则任务会在14号触发。如果15好是周日,则任务会在周一也就是16号触发。如果是在日期域填写“1W”即使1号是周六,那么任务也只会在下周一,也就是3号触发,“W”字符指定的最近工作日是不能够跨月份的。字符“W”只能配合一个单独的数值使用,不能够是一个数字段,如:1-15W是错误的。
     73 
     74 “L”和“W”可以在日期域中联合使用,LW表示这个月最后一周的工作日。
     75 
     76 字符“#”只允许在星期域中出现。这个字符用于指定本月的某某天。例如:“6#3”表示本月第三周的星期五(6表示星期五,3表示第三周)。“2#1”表示本月第一周的星期一。“4#5”表示第五周的星期三。
     77 
     78 字符“C”允许在日期域和星期域出现。这个字符依靠一个指定的“日历”。也就是说这个表达式的值依赖于相关的“日历”的计算结果,如果没有“日历”关联,则等价于所有包含的“日历”。如:日期域是“5C”表示关联“日历”中第一天,或者这个月开始的第一天的后5天。星期域是“1C”表示关联“日历”中第一天,或者星期的第一天的后1天,也就是周日的后一天(周一)。
     79 
     80 
     81 
     82 表达式意义   
     83 "0 0 12 * * ?" 每天中午12点触发   
     84 "0 15 10 ? * *" 每天上午10:15触发   
     85 "0 15 10 * * ?" 每天上午10:15触发   
     86 "0 15 10 * * ? *" 每天上午10:15触发   
     87 "0 15 10 * * ? 2005" 2005年的每天上午10:15触发   
     88 "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发   
     89 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发   
     90 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发   
     91 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发   
     92 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发   
     93 "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发   
     94 "0 15 10 15 * ?" 每月15日上午10:15触发   
     95 "0 15 10 L * ?" 每月最后一日的上午10:15触发   
     96 "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发   
     97 "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发   
     98 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发   
     99 每天早上6点   
    100 0 6 * * *   
    101 每两个小时   
    102 0 */2 * * *   
    103 晚上11点到早上8点之间每两个小时,早上八点   
    104 0 23-7/2,8 * * *   
    105 每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点   
    106 0 11 4 * 1-3  
    107 1月1日早上4点   
    108 0 4 1 1 *-->
    109 </beans>
    
    
    
     

    4、添加的其他的配置

    log4j文件添加:-日志打印

    1 log4j.logger.com.fh.test.quartz=ALL

     注:代码已经同步到github   地址:https://github.com/aaafenghao/com.fh.test.git

  • 相关阅读:
    SIP 研究 API中文
    关于“ARC forbids explicit message send of release”错误
    Android获取屏幕尺寸和密度
    ScrollView 判断滑动到底部
    搭建JAVA版的webService
    MTK 开发
    android 2D动画实现
    android notification详解
    android 监听电话来去电
    SharePoint
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/6937062.html
Copyright © 2011-2022 走看看