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…

package com.mkyong.common;public class RunMeTask
{public void printMe() {System.out.println("Run Me ~");}
}

<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.

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

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

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

2. TimerFactoryBean

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

<bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref local="timerTask" /></list></property>
</bean>

File : Spring-Scheduler.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="schedulerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"><property name="targetObject" ref="runMeTask" /><property name="targetMethod" value="printMe" />
</bean><bean id="runMeTask" class="com.mkyong.common.RunMeTask" /><bean id="timerTask"class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="timerTask" ref="schedulerTask" /><property name="delay" value="1000" /><property name="period" value="60000" />
</bean><bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref local="timerTask" /></list></property>
</bean></beans>

Run it

package com.mkyong.common;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App
{public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Scheduler.xml");}
}

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.

转载于:https://www.cnblogs.com/davidwang456/p/4250403.html

Spring + JDK Timer Scheduler Example--reference相关推荐

  1. Spring-JDK Timer 以及在Spring(4.0以下)中使用JDK Timer

    概述 Timer 和 TimerTask 抽象类TimerTask Timer Timer构造函数及方法 示例 Spring对Java Timer的支持 Spring40已经不支持了推荐使用Quart ...

  2. Spring Boot 3.0.0-M1 Reference Documentation(Spring Boot中文参考文档)-附录A-C

    附录 附录A:常用的应用程序属性 多种属性可以指定到application.properties文件,application.yml文件内,或者作为命令行开关.这个附录提供常用的Spring Boot ...

  3. Spring Boot 3.0.0-M1 Reference Documentation(Spring Boot中文参考文档) 9-16

    9. 数据 Spring Boot与多个数据技术集成,包括SQL和NoSQL. 9.1. SQL数据库 Spring Framework提供扩展支持用于与SQL数据工作,从使用JdbcTemplate ...

  4. Spring 中的Scheduler

    2019独角兽企业重金招聘Python工程师标准>>> Spring Scheduler里有两个概念: 任务(Task) 运行任务的框架(TaskExecutor/TaskSched ...

  5. Spring JDK动态代理

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

  6. spring boot 2.5.5 reference chapter 5 生词

    5 Upgrading Spring Boot check the "migration guide" on the project wiki that provides deta ...

  7. spring jdk动态代理、Cglib动态代理和LoadTimeWeaver(LTW)的应用选择

    在Java 语言中,从织入切面的方式上来看,存在三种织入方式:编译期织入.类加载期织入和运行期织入.编译期织入是指在Java编译期,采用特殊的编译器,将切面织入到Java类中:而类加载期织入则指通过特 ...

  8. Spring JDK内置类型

    String+8种基本类型 <property name="id"><value>10</value> </property> &l ...

  9. Spring之Timer

    表达式 说明秒 分 时 日 月 星期 年 0 0 12 * * ? * 每天中午 12 点触发0 15 10 ? * * 每天上午 10:15 触发 0 15 10 * * ? 每天上午 10:15 ...

最新文章

  1. 关于Redis的使用!
  2. Spring详解(一):简介
  3. AndroidStudio报错:Emulator: I/O warning : failed to load external entity file:/C:/Users/Administrator
  4. Linux系统编程15:进程控制之如何创建进程和写时拷贝技术
  5. Stream从Python切换到Go的原因
  6. Js获取当前系统时间,24小时制
  7. 深度学习2.0-45.GAN实战
  8. 六石编程学:方便调试输出的标准C代码
  9. tomcat6url请求400错误(%2F与%5C)
  10. windows中如何识别RTL8111B/8111C/8111D/8111E/8111F网卡 转自网络
  11. 光流法+FAST特征点
  12. 友链——一群dalao
  13. 假如我来架构12306网站(一) - 概论
  14. 家庭教育的重要性,家庭教育是一切教育的基石
  15. 7-21 输出大写英文字母 (15分)
  16. golang使用mongoDB - mgo.v2
  17. 游戏鼠标的dpi测试软件,教你自己测试鼠标的DPI
  18. 什么是Java集合?
  19. pandas 常用的数学统计方法 mad()
  20. SpringBoot部署应用到本地k8s

热门文章

  1. php worker类,Workerman进阶之Worker类-id属性研究
  2. python调用函数传参时、有默认值的在中间 报错了_python的大坑:使用空列表作为默认参数,让我怀疑遇到了灵异代码...
  3. textview 背景变形_重庆新中式床背景品牌
  4. LED 模板驱动程序的改造:总线设备驱动模型
  5. python生成日历书上哪里错了_python生成日历 - osc_a5pzxo31的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. 中object转为list集合_java基础集合小结
  7. elasticsearch最大节点数_Elasticsearch选举原理之Bully算法
  8. torch_geometric笔记:数据集 ENZYMES Minibatches
  9. NLP学习笔记:word2vec
  10. 博弈论笔记:不完全信息与声誉