Spring定时器的使用

定时执行任务,这是项目中常用的东西,今天我们来做一个使用Spring定时器进行任务定制的小例子,仅供学习!

  1. 首先要增加相应的JAR。
    因为这是一个小例子,使用的JAR包不是很多,用到了spring.jar,quartz-all-1.6.5.jar,quartz-1.5.2.jar,commons-logging.jar,log4j-1.2.14.jar!不用关心版本,从你下载到的Spring中找即可。
  2. 定义web.xml配置文件
    要在配置文件中定义Spring和Log4j的使用。具体看工程即可。重点关注的是如果你做例子时使用了web-app_2_5.xsd,那么在部分服务器上是跑不起来的。
    Xml代码  收藏代码
    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下面。里面需要定义四个内容,具体看注释。重点是时间间隔的配置,这个你可以网上查一下,或看readme文件。
    Xml代码  收藏代码
    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. 执行的代码
    这个代码就是一个普通的代码,里面定义一个要执行的方法就可以了,方法名称自己定义并在Spring配置文件中配置即可。
    Java代码  收藏代码
    1. package com.test;  
    2. import java.text.SimpleDateFormat;  
    3. import java.util.Date;  
    4. /** 
    5.  * 执行类 
    6.  */  
    7. public class Test {  
    8.     // 执行参数  
    9.     private String para ;  
    10.     // 执行方法  
    11.     public void run() {  
    12.         // 定义输出的格式化日期,以便于区分调用  
    13.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    14.         System.out.println("the para is : " + para + " ! Time is :" + format.format(new Date()));  
    15.     }     
    16.     public String getPara() {  
    17.         return para;  
    18.     }     
    19.     public void setPara(String para) {  
    20.         this.para = para;  
    21.     }     
    22. }  
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值