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. Show username instead of "System Account" in SharePoint 2010

    Problems: When I load my local SharePoint site, the account always show as "System Account" ...

  2. bzoj3144

    最小割解决最优值最突出的特点就是要将对象划分到两个集合中这题很明显,裸的最小割先把点连成一根根柱子,就是p(x,y,k)-->p(x,y,k+1)流量是P(x,y,k+1)的不和谐值然后s与p( ...

  3. 以编程方式使用 Word 中的内置对话框

    使用 Microsoft Office Word 时,有时需要显示用户输入对话框.虽然可以创建自己的对话框,您也许还希望采用使用 Word 中内置对话框的方法,这些对话框在Application 对象 ...

  4. CodeForces 400

    A - Inna and Choose Options Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

  5. [转] ICPC2013 World Finals赛后感

    原文地址:http://blog.sina.com.cn/s/blog_6c7729450101lmll.html Orz... 26号考完最后一科计网,27号准备了一下各种材料,28号凌晨就踏上旅程 ...

  6. Java笔记(一)……概述

    一.Java是什么 Java是SUN(Stanford University Network,斯坦福大学网络公司)1995年推出的一门高级编程语言. 二.Java的发展简史 在20世纪90年代初,Su ...

  7. AIX 添加开机启动项

    在学习本主题之前,还是先学习一些AIX关于启动的基础知识,如果你只关心如何开机自动重启程序,请跳过第一章.第二章. ----------------------------第一章 AIX开机启动过程介 ...

  8. Linux下安装mysql5.6.11(找点有用的信息太费劲)(转)

    Linux下安装mysql5.6.11(找点有用的信息太费劲) (2013-04-25 10:25:09)     1.申请阿里云Linux服务器 昨天在阿里云申请了一个免费试用5天的Linux云服务 ...

  9. Storm系列(四)Topology提交校验过程

    功能:提交一个新的Topology,并为Topology创建storm-id(topology-id),校验其结构,设置必要的元数据,最后为Topology分配任务. 实现源码: 1  ); Conf ...

  10. HW1.2

    public class Solution { public static void main(String[] args) { System.out.println("Welcome to ...