一. 前言

近期在公司的项目中用到了定时任务, 本篇博文将会对TimerTask定时任务进行总结, 事实上TimerTask在实际项目中用的不多,

由于它不能在指定时间执行, 仅仅能让程序依照某一个频度执行.



二. TimerTask

JDK中Timer是一个定时器类, 它能够为指定的定时任务进行配置.

JDK中TimerTask是一个定时任务类, 该类实现了Runnable接口, 是一个抽象类, 我们能够继承这个类, 实现定时任务.

/**
* 继承TimerTask实现定时任务
*/
public class MyTask extends TimerTask { @Override
public void run() {
String currentTime = new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(new Date());
System.out.println(currentTime + " 定时任务正在运行...");
} public static void main(String[] args) {
Timer timer = new Timer(); // 1秒钟运行一次的任务, 參数为: task, delay, peroid
timer.schedule(new MyTask(), 2000, 1000);
}
}

三. 整合Spring

两个核心类: ScheduledTimerTask, TimerFactoryBean

ScheduledTimerTask类是对TimerTask的包装器实现, 通过该类能够为这个任务定义触发器信息.

TimerFactoryBean类能够让Spring使用配置创建触发器, 并为一组指定的ScheduledTimerTask bean自己主动创建Timer实例.

1. 引入Jar包: spring.jar, commons-logging.jar

2. 定时调度业务类:

/**
* 定时调度业务类
*/
public class TaskService extends TimerTask {
private int count = 1; public void run() {
System.out.println("第" + count + "次运行定时任务");
count++;
}
}

3. 核心配置:

<?xml version="1.0" encoding="UTF-8"?>
<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="taskService" class="com.zdp.service.TaskService"></bean>
<bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="taskService" /> <!-- 每隔一天运行一次配置: 24*60*60*1000 -->
<!-- 每1秒钟程序运行一次 -->
<property name="period" value="1000" /> <!-- 程序启动4秒钟后開始运行 -->
<property name="delay" value="4000" />
</bean> <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledTimerTask" />
</list>
</property>
</bean>
</beans>

4. 測试类:

public class Main {
public static void main(String[] args) {
// 载入spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("<<-------- 启动定时任务 -------- >>");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
if (reader.readLine().equals("quit")) {
System.out.println("<<-------- 退出定时任务 -------- >>");
System.exit(0);
}
} catch (IOException e) {
throw new RuntimeException("error happens...", e);
}
}
}
}

Spring整合TimerTask实现定时任务调度的更多相关文章

  1. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  2. spring timetask 定时任务调度

    作者:Garry1115 定时任务调度即在设置的特定时间执行特定的任务,不需要人工干预. spring timertask spring 自身所带定时任务类,不需要引入第三方jar包,使用方式如下: ...

  3. quartz任务调度框架与spring整合

    Quartz是什么? Quartz 是一种功能丰富的,开放源码的作业调度库,可以在几乎任何Java应用程序集成 - 从最小的独立的应用程序到规模最大电子商务系统.Quartz可以用来创建简单或复杂的日 ...

  4. Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置

    Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置 >>>>>>>>>>>>&g ...

  5. 基于Spring Task的定时任务调度器实现

    在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...

  6. spring整合quartz时间任务调度框架

    spring整合quartz框架 1.创建maven工程 2.导入jar包(pom.xml) <dependencies> <dependency> <groupId&g ...

  7. Spring整合ActiveMQ实现消息延迟投递和定时投递

    linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...

  8. spring整合quartz并持久化

    spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overd ...

  9. Java定时任务调度详解

    前言 在实际项目开发中,除了Web应用.SOA服务外,还有一类不可缺少的,那就是定时任务调度.定时任务的场景可以说非常广泛,比如某些视频网站,购买会员后,每天会给会员送成长值,每月会给会员送一些电影券 ...

随机推荐

  1. Python基本数据类型之数字int

    数字 int(x, base=None) 将x转换为一个整数.base为按照多少进制进行转换 float(x) 将x转换到一个浮点数. complex(x) 将x转换到一个复数,实数部分为 x,虚数部 ...

  2. python学习-字符串 列表 元祖

    目录 Python翻转字符串(reverse string) 简单的步长为-1, 即字符串的翻转(常用) 递归反转 借用列表,使用reverse()方法 字符串常用操作 index split 切片 ...

  3. 兼容各个浏览器的jquyer zclip复制文本插件 无效的解决办法

    项目中使用点击文本复制功能,用了这个兼容各个浏览器的插件,但是发现放在最前面正常,放到嵌套的html中就失效. 解决办法: <span style="position: relativ ...

  4. Analyzing resource wait related to memory/IO bottleneck

    Analyzing resource wait related to memory bottleneck (RESOURCE_SEMAPHORE, PAGEIOLATCH_XX) sys.dm_os_ ...

  5. BS程序性能调优

    首先想到的是优化算法.改进技术.扩展设备去做优化.其实在讨论性能的时候,绕不开对业务的理解,不同的业务系统对性能的要求不同,优化方式也不一样.优化性能的前提是保证业务的正确性.我们平时关注的性能主要是 ...

  6. 动画库animate.css的用法

    简介 animate.css是一个来自国外的 CSS3 动画库,它预设了引起弹跳(bounce).摇摆(swing).颤抖(wobble).抖动(shake).闪烁(flash).翻转(flip).旋 ...

  7. 资源帖:CV代码库搜集

    2013计算机视觉代码合集一: 原文链接:http://www.yuanyong.org/blog/cv/cv-code-one 切记:一定要看原文链接 原文链接: http://blog.csdn. ...

  8. Computer Vision的尴尬

    原文: Computer Vision是AI的一个非常活跃的领域,每年大会小会不断,发表的文章数以千计(单是CVPR每年就录取300多,各种二流会议每年的文章更可谓不计其数),新模型新算法新应用层出不 ...

  9. 4.4.2 OpenGL几何变换编程实例

    程序运行结果如下图: #include <GL/glut.h> #include <stdlib.h> #include <math.h> /* 初始化显示窗口大小 ...

  10. codeforces 468B two set(并查集)

    链接 B. Two Sets 题意 给两个集合A B,两个数a b,n个数x,分配n个数到两个集合,要求x , a-x在同一个集合,x , b-x在同一个集合,属于A集合的数输出0,B的输出1,无解输 ...