spring quartz定时器的简单配置和使用


第一步:导入相关jar包

(注:单单是在后台执行需要的jar包,若是经过tomcat执行,需额外添加一个jar包——jta-1.1.jar)

不同版本需要依赖的jar:

quartz-all-1.6.0.jar版本需要的jar包:

commons-collections-3.2.jar

commons-logging-1.1.1.jar

log4j-1.2.16.jar

spring.jar

quartz-1.8.4.jar版本需要的jar包:

commons-collections-3.2.jar

commons-logging-1.1.1.jar

log4j-1.2.16.jar

quartz-1.8.4.jar

slf4j-api-1.6.1.jar

slf4j-log4j12-1.6.1.jar

spring.jar

第二步: 新建立一个业务bean-->cn.yulon.service.MessageService

package cn.yulon.service;

public class MessageService {

int i;

public void printLog(){

i++;

System.out.println("this is my timer:" +i);

}

第三步:在Spring配置文件time-bean.xml,如下

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans >

&lt;!-- 第一步: 配置好要定时调用的业务类 --&gt;

<bean id="messageService" class="cn.yulon.service.MessageService" />

&lt;!-- 第二步: 定义好具体要使用类的哪一个业务方法 --&gt;

<bean id="printTimerJob"

class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

&lt;!-- 目标bean --&gt;

<property name="targetObject" ref="messageService" />

&lt;!-- 要执行目标bean的哪一个业务方法 --&gt;

<property name="targetMethod" value="printLog" />

&lt;!-- 是否并发 --&gt;

<property name="concurrent" value="false"/>

</bean>

&lt;!-- 第三步: 定义好调用模式: 如每隔2秒钟调用一次或每天的哪个时间调用一次等 --&gt;

<bean id="printTimerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail" ref="printTimerJob" />

<property name="cronExpression" value="0/2 * * * * ?" />

</bean>

&lt;!-- 启动定时器 --&gt;

&lt;!--第四步 把定义好的任务放到调度(Scheduler)工厂里面,注意这里的ref bean --&gt;

<bean id="schedulerFactoryBean"

class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="applicationContextSchedulerContextKey" value="applicationContext"/>

<property name="triggers">

<list>

<ref bean="printTimerTrigger" />

</list>

</property>

</bean>

&lt;!-- end --&gt;

</beans>

相关介绍:

&lt;!--

在xml里配置值得关注的是<property name="cronExpression" value="0/1 * * * * ? "/>表示每隔一秒钟执行一次,例子如下:

0 0 10,14,16 * * 每天上午10点,下午2点和下午4点

0 0,15,30,45 * 1-10 * 每月前10天每隔15分钟

30 0 0 1 1 2012 在2012年1月1日午夜过30秒时

0 0 8-5 * MON-FRI 每个工作日的工作时间

- 区间

* 通配符 你不想设置那个字段

--&gt;

&lt;!--

cronExpression的介绍:

按顺序 <value> 秒 分 小时 日期 月份 星期 年<value>

字段 允许值 允许的特殊字符

秒 0-59 , - * /

分 0-59 , - * /

小时 0-23 , - * /

日期 1-31 , - * ? / L W C

月份 1-12 或者 JAN-DEC , - * /

星期 1-7 或者 SUN-SAT , - * ? / L C #

年 (可选)留空,1970-2099 , - * /

“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。

--&gt;

在xml里配置值得关注的是<property name="cronExpression" value="0/1 * * * * ? "/>表示每隔一秒钟执行一次,例子如下:

0 0 10,14,16 * * 每天上午10点,下午2点和下午4点

0 0,15,30,45 * 1-10 * 每月前10天每隔15分钟

30 0 0 1 1 2012 在2012年1月1日午夜过30秒时

0 0 8-5 * MON-FRI 每个工作日的工作时间

- 区间

* 通配符 你不想设置那个字段

第四步:新建测试类SpringTest

package cn.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

public static void main(String[] args) {

ApplicationContext act = new ClassPathXmlApplicationContext("time-bean.xml");

}

}

运行结果如下 :

this is my timer:1

this is my timer:2

this is my timer:3

this is my timer:4

this is my timer:5

web.xml的配置:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

<web-app version="2.5"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

&lt;!-- 加载spring的配置文件(一个或者多个) --&gt;

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:time-bean.xml,applicationContext*.xml</param-value>

</context-param>

&lt;!-- 配置spring监听器(作用就是启动Web容器时,自动装配applicationContext.xml文件的配置信息) --&gt;

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

</web-app>

应用场合: 如做一些定时提醒,定时发送邮件、短信,日志定时备份等应用

猜你喜欢

转载自huyumin.iteye.com/blog/1694532
今日推荐