Spring定时器用法

来源:互联网 发布:自学java经典书籍推荐 编辑:程序博客网 时间:2024/06/06 02:23

定时执行任务,这是项目中常用的东西:

  1. 首先要增加相应的JAR。
    spring.jar,quartz-all-1.6.5.jar,quartz-1.5.2.jar,commons-logging.jar,log4j-1.2.14.jar
  2. 定义web.xml配置文件
    要在配置文件中定义Spring和Log4j的使用。
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!-- 如果定义的是web-app_2_5.xsd,那么在部分服务器上是跑不通过的 -->  
  3. <web-app version="2.4" 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.     <welcome-file-list>  
  9.         <welcome-file>index.jsp</welcome-file>  
  10.     </welcome-file-list>  
  11.       
  12.     <!-- 定义Spring配置文件的加载路径 -->  
  13.     <context-param>  
  14.         <param-name>contextConfigLocation</param-name>  
  15.         <param-value>/WEB-INF/spring.xml</param-value>  
  16.     </context-param>  
  17.     <!-- 定义Log4j日志配置文件 -->  
  18.     <context-param>  
  19.         <param-name>log4jConfigLocation</param-name>  
  20.         <param-value>/WEB-INF/classes/log4j.properties</param-value>  
  21.     </context-param>  
  22.       
  23.     <!-- 定义日志监听 -->  
  24.     <listener>  
  25.         <listener-class>  
  26.             org.springframework.web.util.Log4jConfigListener  
  27.         </listener-class>  
  28.     </listener>  
  29.     <!-- 定义Spring监听 -->  
  30.     <listener>  
  31.         <listener-class>  
  32.             org.springframework.web.context.ContextLoaderListener  
  33.         </listener-class>  
  34.     </listener>     
  35. </web-app>  
3、定义Spring配置文件
      这个文件的位置你可以自己定义,我放到了web-inf下面。里面需要定义四个内容,具体看注释。重点是时间间隔的配置。

  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"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  6.     <!-- 每个定义的任务都要在这里进行引用才能运行 -->  
  7.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  8.         <property name="triggers">  
  9.             <list>  
  10.                 <ref local="BusinessTestTrigger" />  
  11.             </list>  
  12.         </property>  
  13.     </bean>  
  14.     <!-- 定义我们要运行的类,可以使用注入定制一些参数 -->  
  15.     <bean id="BusinessTestTime" class="com.test.Test">  
  16.         <property name="para" value="Spring定时器测试V1" />  
  17.     </bean>  
  18.     <!-- 引用,配置要运行的方法 -->  
  19.     <bean id="BusinessTestDetail"  
  20.         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  21.         <property name="targetObject">  
  22.             <ref bean="BusinessTestTime" />  
  23.         </property>  
  24.         <property name="concurrent" value="false" />  
  25.         <property name="targetMethod" value="run" />  
  26.     </bean>  
  27.     <!-- 引用,定制调用间隔,具体时间配置的正则,请阅读readme.txt -->  
  28.     <bean id="BusinessTestTrigger"  
  29.         class="org.springframework.scheduling.quartz.CronTriggerBean">  
  30.         <property name="jobDetail">  
  31.             <ref bean="BusinessTestDetail" />  
  32.         </property>  
  33.         <property name="cronExpression">  
  34.             <value>0/5 * * * * ?</value>  
  35.         </property>  
  36.     </bean>     
  37. </beans>  
4、执行的代码

  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3. /** 
  4.  * 执行类 
  5.  */  
  6. public class Test {  
  7.     // 执行参数  
  8.     private String para ;  
  9.     // 执行方法  
  10.     public void run() {  
  11.         // 定义输出的格式化日期,以便于区分调用  
  12.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  13.         System.out.println("the para is : " + para + " ! Time is :" + format.format(new Date()));  
  14.     }     
  15.     public String getPara() {  
  16.         return para;  
  17.     }     
  18.     public void setPara(String para) {  
  19.         this.para = para;  
  20.     }     
  21. }  
5、发送间隔时间表达式的使用

 位置       含义                                              允许的特殊字符
 1          秒(0-59)                                          , - * /
 2          分(0-59)                                          , - * /
 3          小时(0-23)                                        , - * /
 4          日期(1-31)                                        , - * / ? L C
 5          月                                                (JAN-DEC 或 1-12) , - * /
 6          星期                                              , - * / L C #
 7          年(可选,1970-2099,如果为空则表示全部时间范围)   , - * /
 
通配符和特殊字符的含义
 字符       描述
 *          任意值。使用在表示的任何域表示该值不需要检查。
 ?          无特定值。通常和其它制定的值一起使用,表示一个值必须被显示但是不必检查
 -          范围。例如在小时部分 10-12 ,表示 10,11,12 点
 ,          列分隔符。允许你指定一系列的值,例如在星期域中 MON,TUE,WED
 /          增量。表示一个值的增量。例如在分钟域中 0/1 表示从 0 开始,每次增加一分钟
 L          L是英文Last的缩写。在日期和星期域中意思有一点不同。在日期域中使用代表这个月的最后一天(3月31号,2月29号)。
            使用在星期域中时永远是同一个值:7-Saturday(星期六)。希望使用星期中某一天时可以使用,例如 6L 表示每个月的最后一个周五
 #          允许使用在星期域中,代表这个月的第几个星期。例如 1#2 表示每个月的第一个星期一 
 C          日期值。对于指定日期和星期的支持还没有完成
***********************************************************************************************
时间指定格式:
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>


至少是6个时间元素,最多为7个时间元素
格式描述如下:
秒 0-59
分 0-59
小时 0-23
每月第几天 1-31
月 1-12或JAN-DEC
每个星期第几天 1-7或SUN-SAT
年 1970-2099
注意:每月第几天和每星期第几天是互斥的,两个只能设置一个,不设置的以 ? 符号编写
如果有好几个时间点,可以使用 , 符号,如:0 0 10,12,14 * * ?,表示每天的10点、12点、14点要执行JOB
对于连续的时间可以使用 - 符号,如:0 0 10,12,14 1-15 * ?,表示每月的1到15号10点、12点、14点执行JOB
时间格式中的年可有可无
对于 0/1 * * * * ? 此类写法,则表示每秒执行一次JOB,也可以写成如:0 0/5 * * * ? 代表每五分钟执行一次JOB


原创粉丝点击