http://www.mkyong.com/spring/spring-jdk-timer-scheduler-example/

In this example, you will use Spring’s Scheduler API to schedule a task.

1. Scheduler Task

Create a scheduler task…

  1. package com.mkyong.common;
  2.  
  3. public class RunMeTask
  4. {
  5. public void printMe() {
  6. System.out.println("Run Me ~");
  7. }
  8. }
  1. <bean id="runMeTask" class="com.mkyong.common.RunMeTask" />

Spring comes with a MethodInvokingTimerTaskFactoryBean as a replacement for the JDK TimerTask. You can define your target scheduler object and method to call here.

  1. <bean id="schedulerTask"
  2. class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
  3. <property name="targetObject" ref="runMeTask" />
  4. <property name="targetMethod" value="printMe" />
  5. </bean>

Spring comes with a ScheduledTimerTask as a replacement for the JDK Timer. You can pass your scheduler name, delay and period here.

  1. <bean id="timerTask"
  2. class="org.springframework.scheduling.timer.ScheduledTimerTask">
  3. <property name="timerTask" ref="schedulerTask" />
  4. <property name="delay" value="1000" />
  5. <property name="period" value="60000" />
  6. </bean>

2. TimerFactoryBean

In last, you can configure a TimerFactoryBean bean to start your scheduler task.

  1. <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
  2. <property name="scheduledTimerTasks">
  3. <list>
  4. <ref local="timerTask" />
  5. </list>
  6. </property>
  7. </bean>

File : Spring-Scheduler.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="schedulerTask"
  7. class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
  8. <property name="targetObject" ref="runMeTask" />
  9. <property name="targetMethod" value="printMe" />
  10. </bean>
  11.  
  12. <bean id="runMeTask" class="com.mkyong.common.RunMeTask" />
  13.  
  14. <bean id="timerTask"
  15. class="org.springframework.scheduling.timer.ScheduledTimerTask">
  16. <property name="timerTask" ref="schedulerTask" />
  17. <property name="delay" value="1000" />
  18. <property name="period" value="60000" />
  19. </bean>
  20.  
  21. <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
  22. <property name="scheduledTimerTasks">
  23. <list>
  24. <ref local="timerTask" />
  25. </list>
  26. </property>
  27. </bean>
  28.  
  29. </beans>

Run it

  1. package com.mkyong.common;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class App
  7. {
  8. public static void main( String[] args )
  9. {
  10. ApplicationContext context =
  11. new ClassPathXmlApplicationContext("Spring-Scheduler.xml");
  12. }
  13. }

No code need to call the scheduler task, the TimerFactoryBean will run your schedule task during start up. As result, Spring scheduler will run the printMe() method every 60 seconds, with a 1 second delay for the first time of execution.

 
 

Spring + JDK Timer Scheduler Example--reference的更多相关文章

  1. JDK Timer & TimerTask

    目录 Timer & TimerTask Binary Heap Insert DELETE MIN PERFORMANCE LifeCycle Constructor MainLoop sc ...

  2. JDK源码阅读-Reference

    本文转载自JDK源码阅读-Reference 导语 Java最初只有普通的强引用,只有对象存在引用,则对象就不会被回收,即使内存不足,也是如此,JVM会爆出OOME,也不会去回收存在引用的对象. 如果 ...

  3. Spring JDK动态代理

    1. 创建项目在 MyEclipse 中创建一个名称为 springDemo03 的 Web 项目,将 Spring 支持和依赖的 JAR 包复制到 Web 项目的 WEB-INF/lib 目录中,并 ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring JDK动态代理

    JDK 动态代理是通过 JDK 中的 java.lang.reflect.Proxy 类实现的.下面通过具体的案例演示 JDK 动态代理的使用. 1. 创建项目 在 MyEclipse 中创建一个名称 ...

  5. spring集成quartz scheduler

    创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean  Quartz Scheduler 配置作业(MethodInvokingJobD ...

  6. Spring Boot TImer Schedule Quartz

    Spring Boot 2.X(十二):定时任务-云栖社区-阿里云https://yq.aliyun.com/articles/723876?spm=a2c4e.11155472.0.0.2f8b3a ...

  7. 项目中使用Quartz集群分享--转载

    项目中使用Quartz集群分享--转载 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享  一:CRM对定时任务的依赖与问题  二:什么是quartz,如何使用, ...

  8. Spring 3 调度器示例 —— JDK 定时器和 Quartz 展示

    Spring框架提供了执行和调度任务的抽象,支持线程池或者在应用服务器环境中代理给CommonJ. Spring也集成了支持使用JDK Timer和Quartz调度库提供的Quartz Schedul ...

  9. Spring Boot Reference Guide

    Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch,  ...

随机推荐

  1. Little-endian的一个好处:在变量指针转换的时候地址保持不变

    Big-endian 的内存顺序和数字的书写顺序是一致的,方便阅读理解.Little-endian 在变量指针转换的时候地址保持不变,比如 int64* 转到 int32* 各有利弊,统一就好,目前看 ...

  2. 《ArcGIS Engine+C#实例开发教程》第八讲 属性数据表的查询显示

    原文:<ArcGIS Engine+C#实例开发教程>第八讲 属性数据表的查询显示 第一讲 桌面GIS应用程序框架的建立 第二讲 菜单的添加及其实现 第三讲 MapControl与Page ...

  3. js中replace用法

    js中replace的用法 replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则 ...

  4. FastJSON学习

    这几天在用FastJSON,发现需要测试一些关键点,包括: 1.是否支持内部类:测试结果是支持,但是需要设置为静态类(static) 2.是否支持继承的自动序列化及反序列化:测试结果是支持 3.缺字段 ...

  5. poj3373Changing Digits(dp)

    链接 dfs倒着搜 返回的路径不能满足相同的数最多 借鉴了下别人的代码.. 先dp出来 再倒着标记一下 然后正回来一定可以满足了 dp保存的是最小的不相同数 #include <iostream ...

  6. Redis 如何导出数据

    Redis是一款支持多种数据类型的Key-Value数据库. 这里介绍下如何从Redis中导出数据. 数据是如何存储的?. Redis中是把数据保存到内存中的,但是它也会定期的把数据写会到硬盘中. R ...

  7. cnb

    <style type="text/css"> .fieldset_s{border: 1px #dedede solid;padding: 19px; color: ...

  8. SQL中取当前记录的ID----->SCOPE_IDENTITY()

    SQL Server 2000中,有三个比较类似的功能:他们分别是:SCOPE_IDENTITY.IDENT_CURRENT 和 @@IDENTITY,它们都返回插入到 IDENTITY 列中的值.I ...

  9. POJ --- 3613 (K步最短路+矩阵快速幂+floyd)

    Cow Relays   Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided ...

  10. Mealy和moore型状态机

    Moore型状态机:下一状态只由当前状态决定,即次态=f(现状,输入),输出=f(现状): Mealy型状态机:下一状态不但与当前状态有关,还与当前输入值有关,即次态=f(现状,输入),输出=f(现状 ...