Quartz与Spring强强联手,定时任务实现更容易

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!");
        }
}
 
二、配置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 >
 
三、写测试
 
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方法执行结束了!");
        }
}
 
运行测试:
Main方法执行开始了! 定时器伴随着Spring的初始化执行了。。。
Main方法执行结束了!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
Just Do It!
......
 
运行结果表名:在Spring初始化后,定时器根据延迟时间启动执行,并且在执行过过程中,Main线程一直没有退出。定时器线程一直在执行。
 
在强行终止Main线程的时候,定时器也停止执行。
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值