定时执行任务,这是项目中常用的东西,今天我们来做一个使用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中找即可。
定义web.xml配置文件
要在配置文件中定义Spring和Log4j的使用。
具体看工程即可。
重点关注的是如果你做例子时使用了web-app_2_5.xsd,那么在部分服务器上是跑不起
来的。
Xml代码
1<?xml version="1.0" encoding="UTF-8"?>
1<!-- 如果定义的是web-app_2_5.xsd,那么在部分服务器上是跑不通过的
-->
1<web-app version="2.4" xmlns="/xml/ns/j2ee"
1xmlns:xsi="/2001/XMLSchema-instance"
1xsi:schemaLocation="/xml/ns/j2ee
1/xml/ns/j2ee/web-app_2_4.xsd">
1<!-- 系统默认首页面-->
1<welcome-file-list>
1<welcome-file>index.jsp</welcome-file>
1</welcome-file-list>
1
1<!-- 定义Spring配置文件的加载路径-->
1<context-param>
1<param-name>contextConfigLocation</param-name>
1<param-value>/WEB-INF/spring.xml</param-value>
1</context-param>
1<!-- 定义Log4j日志配置文件-->
1<context-param>
1<param-name>log4jConfigLocation</param-name>
1<param-value>/WEB-INF/classes/log4j.properties</param-value> 1</context-param>
1
1<!-- 定义日志监听-->
1<listener>
1<listener-class>
1org.springframework.web.util.Log4jConfigListener
1</listener-class>
1</listener>
1<!-- 定义Spring监听-->
1<listener>
1<listener-class>
1org.springframework.web.context.ContextLoaderListener
1</listener-class>
1</listener>
1</web-app>
定义Spring配置文件
这个文件的位置你可以自己定义,我放到了web-inf下面。
里面需要定义四个内容,具体看注释。
重点是时间间隔的配置,这个你可以网上查一下,或看
readme文件。
Xml代码
1<?xml version="1.0" encoding="UTF-8"?>
1<beans xmlns="/schema/beans"
1xmlns:xsi="/2001/XMLSchema-instance"
1xsi:schemaLocation="/schema/beans
1/schema/beans/spring-beans-2.0.xsd">
1<!-- 每个定义的任务都要在这里进行引用才能运行-->
1<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
1<property name="triggers">
1<list>
1<ref local="BusinessTestTrigger" />
1</list>
1</property>
1</bean>
1<!-- 定义我们要运行的类,可以使用注入定制一些参数-->
1<bean id="BusinessTestTime" class="com.test.Test">
1<property name="para" value="Spring定时器测试V1" />
1</bean>
1<!-- 引用,配置要运行的方法-->
1<bean id="BusinessTestDetail"
1class="org.springframework.scheduling.quartz.MethodInvokingJobDetailF actoryBean">
1<property name="targetObject">
1<ref bean="BusinessTestTime" />
1</property>
1<property name="concurrent" value="false" />
1<property name="targetMethod" value="run" />
1</bean>
1<!-- 引用,定制调用间隔,具体时间配置的正则,请阅读readme.txt --> 1<bean id="BusinessTestTrigger"
1class="org.springframework.scheduling.quartz.CronTriggerBean">
1<property name="jobDetail">
1<ref bean="BusinessTestDetail" />
1</property>
1<property name="cronExpression">
1<value>0/5 * * * * ?</value>
1</property>
1</bean>
1</beans>
执行的代码
这个代码就是一个普通的代码,里面定义一个要执行的方法就可以了,方法
名称自己定义并在Spring配置文件中配置即可。
Java代码
1package com.test;
1import java.text.SimpleDateFormat;
1import java.util.Date;
1/**
1* 执行类
1*/
1public class Test {
1// 执行参数
1private String para ;
1// 执行方法
1public void run() {
1// 定义输出的格式化日期,以便于区分调用
1SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
1System.out.println("the para is : " + para + " ! Time is :" +
format.format(new Date()));
1}
1public String getPara() {
1return para;
1}
1public void setPara(String para) {
1this.para = para;
1}
1}
备注:工程直接使用工具导入即可查看代码,将工程部署即可在控制台看到效果。