Spring + JDK Timer Scheduler Example--reference
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.
Spring + JDK Timer Scheduler Example--reference的更多相关文章
- JDK Timer & TimerTask
目录 Timer & TimerTask Binary Heap Insert DELETE MIN PERFORMANCE LifeCycle Constructor MainLoop sc ...
- JDK源码阅读-Reference
本文转载自JDK源码阅读-Reference 导语 Java最初只有普通的强引用,只有对象存在引用,则对象就不会被回收,即使内存不足,也是如此,JVM会爆出OOME,也不会去回收存在引用的对象. 如果 ...
- Spring JDK动态代理
1. 创建项目在 MyEclipse 中创建一个名称为 springDemo03 的 Web 项目,将 Spring 支持和依赖的 JAR 包复制到 Web 项目的 WEB-INF/lib 目录中,并 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring JDK动态代理
JDK 动态代理是通过 JDK 中的 java.lang.reflect.Proxy 类实现的.下面通过具体的案例演示 JDK 动态代理的使用. 1. 创建项目 在 MyEclipse 中创建一个名称 ...
- spring集成quartz scheduler
创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean Quartz Scheduler 配置作业(MethodInvokingJobD ...
- Spring Boot TImer Schedule Quartz
Spring Boot 2.X(十二):定时任务-云栖社区-阿里云https://yq.aliyun.com/articles/723876?spm=a2c4e.11155472.0.0.2f8b3a ...
- 项目中使用Quartz集群分享--转载
项目中使用Quartz集群分享--转载 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享 一:CRM对定时任务的依赖与问题 二:什么是quartz,如何使用, ...
- Spring 3 调度器示例 —— JDK 定时器和 Quartz 展示
Spring框架提供了执行和调度任务的抽象,支持线程池或者在应用服务器环境中代理给CommonJ. Spring也集成了支持使用JDK Timer和Quartz调度库提供的Quartz Schedul ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
随机推荐
- Little-endian的一个好处:在变量指针转换的时候地址保持不变
Big-endian 的内存顺序和数字的书写顺序是一致的,方便阅读理解.Little-endian 在变量指针转换的时候地址保持不变,比如 int64* 转到 int32* 各有利弊,统一就好,目前看 ...
- 《ArcGIS Engine+C#实例开发教程》第八讲 属性数据表的查询显示
原文:<ArcGIS Engine+C#实例开发教程>第八讲 属性数据表的查询显示 第一讲 桌面GIS应用程序框架的建立 第二讲 菜单的添加及其实现 第三讲 MapControl与Page ...
- js中replace用法
js中replace的用法 replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则 ...
- FastJSON学习
这几天在用FastJSON,发现需要测试一些关键点,包括: 1.是否支持内部类:测试结果是支持,但是需要设置为静态类(static) 2.是否支持继承的自动序列化及反序列化:测试结果是支持 3.缺字段 ...
- poj3373Changing Digits(dp)
链接 dfs倒着搜 返回的路径不能满足相同的数最多 借鉴了下别人的代码.. 先dp出来 再倒着标记一下 然后正回来一定可以满足了 dp保存的是最小的不相同数 #include <iostream ...
- Redis 如何导出数据
Redis是一款支持多种数据类型的Key-Value数据库. 这里介绍下如何从Redis中导出数据. 数据是如何存储的?. Redis中是把数据保存到内存中的,但是它也会定期的把数据写会到硬盘中. R ...
- cnb
<style type="text/css"> .fieldset_s{border: 1px #dedede solid;padding: 19px; color: ...
- SQL中取当前记录的ID----->SCOPE_IDENTITY()
SQL Server 2000中,有三个比较类似的功能:他们分别是:SCOPE_IDENTITY.IDENT_CURRENT 和 @@IDENTITY,它们都返回插入到 IDENTITY 列中的值.I ...
- POJ --- 3613 (K步最短路+矩阵快速幂+floyd)
Cow Relays Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided ...
- Mealy和moore型状态机
Moore型状态机:下一状态只由当前状态决定,即次态=f(现状,输入),输出=f(现状): Mealy型状态机:下一状态不但与当前状态有关,还与当前输入值有关,即次态=f(现状,输入),输出=f(现状 ...