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的更多相关文章

  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. 产品不应该大而全,而是应该小而精(DropBox有感,产品要1分钟学会)

    昨天试用了一下DROPBOX的个人版,对它的功能与界面简单深感震惊. 后来与一位业内朋友交流了一下,他说: 产品一般都是通过一个点来做.把一个点做到最好有可能会成为平台.另外还要在合适的时间做合适的事 ...

  2. Form.KeyPreview 属性

    Form.KeyPreview 属性 今天再做KeyDown 和 KeyUp 事件时,就是忘了设置,窗体的KeyPreview 属性,所以KeyDown 和 KeyUp 事件没有反应(这里说明一下,本 ...

  3. 【HDOJ】3909 Sudoku

    DLX的应用,基本题,注意maxnode开大点儿. /* 3909 */ #include <iostream> #include <string> #include < ...

  4. IPv6 tutorial 3 New features: IPsec and LAN features

    https://4sysops.com/archives/ipv6-tutorial-part-3-new-features-ipsec-and-lan-features/ In the last p ...

  5. Apache Struts ClassLoader操作漏洞

    漏洞版本: Apache Struts 2.x 漏洞描述: CVE ID:CVE-2014-0094 Struts2 是第二代基于Model-View-Controller (MVC)模型的java企 ...

  6. iOS添加自定义字体方法

    1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...

  7. MySQL优化技巧之五(mysql查询性能优化)

    对于高性能数据库操作,只靠设计最优的库表结构.建立最好的索引是不够的,还需要合理的设计查询.如果查询写得很糟糕,即使库表结构再合理.索引再合适,也无法实现高性能.查询优化.索引优化.库表结构优化需要齐 ...

  8. Java&MySQL Type Mapping

    MySQL Type Name Return value of GetColumnClassName Returned as Java Class BIT(1) (new in MySQL-5.0) ...

  9. Cocos2d-x ios 下http请求的另一种实现

    简单描述下需求:游戏要加入事件log,比如玩家升到10级:创建角色:或是,触发这些事件后要求客户端忘后台抛送一条log信息.一般情况下,我们可以直接使用cocos自带的HttpClient(底层用li ...

  10. [codevs1557]热浪

    本题地址:http://www.luogu.org/problem/show?pid=1339 http://codevs.cn/problem/1557/ http://www.tyvj.cn/p/ ...