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. Linq的延迟

    书名:LINQ: The Future of Data Access in C# 3.0 Learn LINQ and the C# 3.0 Features That Support It http ...

  2. Trigger Execution Sequence in Oracle Forms

    Introduction ------------ This document lists the order in which triggers fire in Oracle Forms 4.5: ...

  3. Memcached‘do_item_get’函数安全漏洞

    漏洞名称: Memcached‘do_item_get’函数安全漏洞 CNNVD编号: CNNVD-201401-175 发布时间: 2014-01-15 更新时间: 2014-01-15 危害等级: ...

  4. apache开源项目--gora

    Gora 是一个应用于 NoSQL 数据库的 ORM 框架,支持包括:Apache HBase/Apache Cassandra

  5. XHTML代码规则&手工html转换xhtml

    XHTML规则 XHTML是XML得一个应用,它遵守XML得规范和要求.从技术角度上讲.这些语法规则是由XML规范定义的. XML文档必须遵守的规则使得生成工具以解析文档变得更容易.这些规则也使得XM ...

  6. How to detect and avoid memory and resources leaks in .NET applications

    By Fabrice Marguerie Despite what a lot of people believe, it's easy to introduce memory and resourc ...

  7. 【JS】Intermediate3:AJAX

    1.load new content into a page without a full reload XML HTTP Request (XHR) To retrieve new content ...

  8. makefile 中 $@ $^ %< 使用

    这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将会学到以下内容: 源程序编译 Makefile的编写 程序库的链接 程序的调试 头文件和系统求助 1.源程序的编译 在L ...

  9. HW4.37

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  10. java.lang.UnsupportedClassVersionError: Unsupported major.minor version 52.0的错误

    1.首先检查是不是jdk版本过低,如果过低的话就把jdk重新安装一下 2.在编译器的版本中设置一下,compiler中设置成与jdk版本相同