Quartz与Spring强强联手,定时任务实现更容易
环境:
Spring 2.5.4
Quartz 1.6.0
Quartz是一个企业级的定时任务执行工具,使用起来也相当容易。但是也有点约束----每个作业必须实现Job接口。
Spring早在1.0就对Quartz提供了支持。Spring AOP的强大功能可以将这个无聊的事情见鬼去吧。我们甚至可以将任何一个普通类的方法设定为定时执行的方法。并且在Spring初始化的自动启动定时器(不需要你去写Main方法),在Spring关闭的时候结束定时器的运行,一避免应用服务器都关闭了,定时器还在后台默默无闻“空转”。呵呵,下面就看个例子吧:
目标:将一个普通的业务方法作为定时作业的方法通过Spring配置去执行。
代码:
第一步:写业务方法
package stu.quartz;
/**
* 业务方法
*
* @author leizhimin,2008-10-8 15:41:52
*/
public class TestService {
public void doSomething() {
System.out.println( "Do Something Freely!");
}
public void justDoIt() {
System.out.println( "Just Do It!");
}
}
/**
* 业务方法
*
* @author leizhimin,2008-10-8 15:41:52
*/
public class TestService {
public void doSomething() {
System.out.println( "Do Something Freely!");
}
public void justDoIt() {
System.out.println( "Just Do It!");
}
}
二、配置Spring
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< beans default-autowire ="byName" xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]" >
<!-- 普通的业务Bean -->
< bean name ="testService" class ="stu.quartz.TestService" />
<!-- 作业 -->
< bean id ="jobDetail_test" class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
< property name ="targetObject" ref ="testService" />
< property name ="targetMethod" value ="justDoIt" />
</ bean >
<!-- 触发器 -->
< bean name ="testTrigger" class ="org.springframework.scheduling.quartz.SimpleTriggerBean" >
< property name ="jobDetail" ref ="jobDetail_test" />
< property name ="startDelay" value ="10" />
< property name ="repeatInterval" value ="2000" />
< property name ="repeatCount" value ="100" />
< property name ="jobDataAsMap" >
< map >
< entry key ="count" value ="10" />
</ map >
</ property >
</ bean >
<!-- 计划 -->
< bean name ="testScheduler" class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
< property name ="triggers" >
< list >
< ref bean ="testTrigger" />
</ list >
</ property >
< property name ="schedulerContextAsMap" >
< map >
< entry key ="timeout" value ="30" />
</ map >
</ property >
<!-- Quartz的高级配置-->
<!-- <property name="configLocation" value="classpath:com/lavasoft/quartz.properties"/>-->
</ bean >
</ beans >
< beans default-autowire ="byName" xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]" >
<!-- 普通的业务Bean -->
< bean name ="testService" class ="stu.quartz.TestService" />
<!-- 作业 -->
< bean id ="jobDetail_test" class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
< property name ="targetObject" ref ="testService" />
< property name ="targetMethod" value ="justDoIt" />
</ bean >
<!-- 触发器 -->
< bean name ="testTrigger" class ="org.springframework.scheduling.quartz.SimpleTriggerBean" >
< property name ="jobDetail" ref ="jobDetail_test" />
< property name ="startDelay" value ="10" />
< property name ="repeatInterval" value ="2000" />
< property name ="repeatCount" value ="100" />
< property name ="jobDataAsMap" >
< map >
< entry key ="count" value ="10" />
</ map >
</ property >
</ bean >
<!-- 计划 -->
< bean name ="testScheduler" class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
< property name ="triggers" >
< list >
< ref bean ="testTrigger" />
</ list >
</ property >
< property name ="schedulerContextAsMap" >
< map >
< entry key ="timeout" value ="30" />
</ map >
</ property >
<!-- Quartz的高级配置-->
<!-- <property name="configLocation" value="classpath:com/lavasoft/quartz.properties"/>-->
</ bean >
</ beans >
三、写测试
package stu.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
*
* @author leizhimin,2008-10-8 16:33:21
*/
public class TestQuartz {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "/Application_quartz.xml");
System.out.println( "Main方法执行开始了! 定时器伴随着Spring的初始化执行了。。。");
System.out.println( "Main方法执行结束了!");
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
*
* @author leizhimin,2008-10-8 16:33:21
*/
public class TestQuartz {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "/Application_quartz.xml");
System.out.println( "Main方法执行开始了! 定时器伴随着Spring的初始化执行了。。。");
System.out.println( "Main方法执行结束了!");
}
}
运行测试:
Main方法执行开始了! 定时器伴随着Spring的初始化执行了。。。
Main方法执行结束了!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
......
Main方法执行结束了!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
......
运行结果表名:在Spring初始化后,定时器根据延迟时间启动执行,并且在执行过过程中,Main线程一直没有退出。定时器线程一直在执行。
在强行终止Main线程的时候,定时器也停止执行。
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/104357,如需转载请自行联系原作者