spring 动态定时器

    真是好久没有学习了呀,学习的方法都快不会了,还好,学习的精神还在,伟东说:不怕慢,就怕站.呵呵.我确实慢了点,但是好歹没有站着.

         新换的项目,也算是新公司吧,感觉乱七八糟的,什么都不在状态。前些天只是开开会而已,然后就没什么了,我和对面的同事基本上闲着待了一个多星期。期间,结合项目需求,有个需要做的事情就是动态定时器。于是扩展了一个知识。

        spring静态定时器,可以看我以前的博客:

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

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

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

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

        习惯了百度,几乎没有自己思考就百度了一下。果然有很多的资料。当然,看了很多都不能直接拿来使用,其中有一篇是将思路也同时写出来的,我就是学习了这篇博客:http://ithead.iteye.com/blog/1460748     http://xiaobingandxiaoer.iteye.com/blog/1178741

     

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

      根据我参考的博客中提到的:定时器的定时的核心在于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>  

更多的理解会在下一篇博客中继续。今天先到此了。

欢迎拍砖!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
Spring框架中,有多种方式可以实现定时任务调度。以下是Spring内置的定时器实现方式: 1. 使用`@Scheduled`注解:在需要执行定时任务的方法上添加`@Scheduled`注解,并配置相应的定时任务表达式。例如: ```java import org.springframework.scheduling.annotation.Scheduled; public class MyScheduledTask { @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行 public void myTask() { // 执行定时任务的逻辑 } } ``` 2. 实现`SchedulingConfigurer`接口:创建一个类实现`SchedulingConfigurer`接口,并重写`configureTasks`方法,用于配置定时任务。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; @Configuration @EnableScheduling public class MyTaskConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // 配置定时任务 taskRegistrar.addCronTask(() -> { // 执行定时任务的逻辑 }, "0 0 0 * * ?"); // 每天凌晨执行 } } ``` 3. 使用XML配置:在Spring的XML配置文件中使用`<task:scheduled>`标签来配置定时任务。例如: ```xml <beans xmlns:task="http://www.springframework.org/schema/task"> <task:scheduler id="myScheduler" pool-size="10" /> <task:scheduled ref="myTaskBean" method="myTask" cron="0 0 0 * * ?" /> <bean id="myTaskBean" class="com.example.MyTaskBean" /> </beans> ``` 这些是Spring内置的一些定时任务调度的实现方式,根据项目的需求和个人偏好,选择适合的方式来实现定时任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值