定时执行任务,这是项目中常用的东西,今天我们来做一个使用Spring定时器进行任务定制的小例子,仅供学习!
首先要增加相应的JAR。
因为这是一个小例子,使用的JAR包不是很多,用到了spring.jar,quartz-all-1.6.5.jar,quartz-1.5.2.jar,commons-logging.jar,log4j-1.2.14.jar!不用关心版本,从你下载到的Spring中找即可。
定义web.xml配置文件
要在配置文件中定义Spring和Log4j的使用。具体看工程即可。重点关注的是如果你做例子时使用了web-app_2_5.xsd,那么在部分服务器上是跑不起来的。
<?xml version="1.0" encoding="UTF-8"?> <!-- 如果定义的是web-app_2_5.xsd,那么在部分服务器上是跑不通过的 --> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 系统默认首页面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 定义Spring配置文件的加载路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring.xml</param-value> </context-param> <!-- 定义Log4j日志配置文件 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <!-- 定义日志监听 --> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- 定义Spring监听 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
定义Spring配置文件
这个文件的位置你可以自己定义,我放到了web-inf下面。里面需要定义四个内容,具体看注释。重点是时间间隔的配置,这个你可以网上查一下,或看readme文件。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 每个定义的任务都要在这里进行引用才能运行 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="BusinessTestTrigger" /> </list> </property> </bean> <!-- 定义我们要运行的类,可以使用注入定制一些参数 --> <bean id="BusinessTestTime" class="com.test.Test"> <property name="para" value="Spring定时器测试V1" /> </bean> <!-- 引用,配置要运行的方法 --> <bean id="BusinessTestDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="BusinessTestTime" /> </property> <property name="concurrent" value="false" /> <property name="targetMethod" value="run" /> </bean> <!-- 引用,定制调用间隔,具体时间配置的正则,请阅读readme.txt --> <bean id="BusinessTestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="BusinessTestDetail" /> </property> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> </beans>
执行的代码
这个代码就是一个普通的代码,里面定义一个要执行的方法就可以了,方法名称自己定义并在Spring配置文件中配置即可。
package com.test; import java.text.SimpleDateFormat; import java.util.Date; /** * 执行类 */ public class Test { // 执行参数 private String para ; // 执行方法 public void run() { // 定义输出的格式化日期,以便于区分调用 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("the para is : " + para + " ! Time is :" + format.format(new Date())); } public String getPara() { return para; } public void setPara(String para) { this.para = para; } }
Java小强
未曾清贫难成人,不经打击老天真。
自古英雄出炼狱,从来富贵入凡尘。
发表评论: