Spring 动态定时器


Spring静态定时器,可以参考:

Spring3.0定时任务 (一) 简单示例

Spring定时任务 (二) 多个任务和执行时间间隔配置

以前一直都用静态的定时器,非常简单,只需要做两件事:1、写执行任务的类和方法;2、写好配置文件。当然执行任务的间隔也是写死在配置文件中了的。对于很多操作,比如备份数据、同步数据等等都可以完成,但是对于更加灵活的定时器就不行了。于是就有了动态定时器的概念。

动态定时器:执行任务的时间由程序自己控制,而不是由配置文件决定。

先说一下我自己的理解和思路,然后贴我的例子:

根据我参考的文章中提到的:定时器的定时的核心在于cronException,也就是我们在配置文件中配置的:

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

如果我们能够将此处动态设置,也就实现了动态定时器。所以,动态定时器的核心在于对于这个参数的设置cronException。静态的是写在配置文件中,动态的是写到程序中,具体怎样动态的写到程序中的呢?看下面的例子:

下面将我自己写的例子贴上,测试运行成功的:

首先是运行环境: spring2.5.6+quartz1.8(spring一般都用3.0以上了,我们这个项目用的2.5而已,亲可以用3.0以上的版本),相关需要引入的jar和配置文件过程此处略过。

代码:

package test;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
 
//执行任务的类
public class ScheduleInfoAction{ 
//    Logger logger = Logger.getLogger(ScheduleInfoAction.class); 
    //执行任务的方法
    public void reScheduleJob()  { 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     System.out.println("执行操作"+dateFormat.format(new Date()));
    } 


package test;

import java.io.Serializable;
import java.text.ParseException;

import org.springframework.scheduling.quartz.CronTriggerBean;

//继承org.springframework.scheduling.quartz.CronTriggerBean;
//父类就是静态定时器中用到的触发器
public class InitCronTrigger extends CronTriggerBean implements Serializable { 
 
    public InitCronTrigger() throws ParseException {
        setCronExpression(getCronExceptionDB());  //在构造方法中设置cronException参数
    }
    private String getCronExceptionDB(){ 
        String sql = "select CRON from t_test_task_trigger where available = 1 and trigger_name = 'cronTrigger'"; 
        System.out.println("*****" + sql); 
        return "* * * 11 12 ?"; //此处作为测试,直接返回结果,可以根据需要从数据库中读取
    } 

配置文件(spring-application-quatz.xml)

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
      default-autowire="no" default-lazy-init="false">
 
    <!-- 配置spring响应触发器的入口 -->
    <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <property name="triggers"> 
            <list> 
                <ref local="cronTrigger"/> 
            </list> 
        </property> 
    </bean> 
    <bean id="scheduleInfoAction" class="test.ScheduleInfoAction"/>
   
   
    <!-- 此处注意:静态定时器的class为: org.springframework.scheduling.quartz.CronTriggerBean
     我用了自己定义的类,此类继承了静态定时器的类,并将执行任务的时间设置放到了类的构造方法中,实现动态定时器的一种方式。
    -->
  <bean id="cronTrigger" class="test.InitCronTrigger">
          <property name="jobDetail" ref="jobDetail"/> 
          <!--
          <property name="cronExpression"> 
              <value>0/10 * * * * ?</value> 
          </property> 
          -->
  </bean>
  <!-- 配置执行任务的类和方法。 -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
        <property name="targetObject" ref="scheduleInfoAction"/><!-- 执行任务的方法 --> 
        <property name="targetMethod" value="reScheduleJob"/><!-- 执行任务的方法,此处方法不带参数,即便实际的方法带参数了,执行过程也会将参数略掉 --> 
        <!-- concurrent设为false,多个job不会并发运行 --> 
        <property name="concurrent" value="false"/> 
    </bean> 
</beans> 

 

 

 

 

相关内容