Spring整合TimerTask实现定时任务调度
一. 前言
近期在公司的项目中用到了定时任务, 本篇博文将会对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.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- spring timetask 定时任务调度
作者:Garry1115 定时任务调度即在设置的特定时间执行特定的任务,不需要人工干预. spring timertask spring 自身所带定时任务类,不需要引入第三方jar包,使用方式如下: ...
- quartz任务调度框架与spring整合
Quartz是什么? Quartz 是一种功能丰富的,开放源码的作业调度库,可以在几乎任何Java应用程序集成 - 从最小的独立的应用程序到规模最大电子商务系统.Quartz可以用来创建简单或复杂的日 ...
- Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置
Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置 >>>>>>>>>>>>&g ...
- 基于Spring Task的定时任务调度器实现
在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...
- spring整合quartz时间任务调度框架
spring整合quartz框架 1.创建maven工程 2.导入jar包(pom.xml) <dependencies> <dependency> <groupId&g ...
- Spring整合ActiveMQ实现消息延迟投递和定时投递
linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...
- spring整合quartz并持久化
spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overd ...
- Java定时任务调度详解
前言 在实际项目开发中,除了Web应用.SOA服务外,还有一类不可缺少的,那就是定时任务调度.定时任务的场景可以说非常广泛,比如某些视频网站,购买会员后,每天会给会员送成长值,每月会给会员送一些电影券 ...
随机推荐
- Linux 定时任务 Crontab按秒执行
目前在crontab中最小执行时间单位为分钟. 如果需要按秒来执行,有以下两种方法: 方法一:通过sleep来实现 例: 1.创建test.php文件,这里测试通过打印时间好区分. <?php ...
- MongoDB Master-Slave cluster with authentication setup
Master Server create mongo db folder with sub folders like data, conf, && log mkdir -p /opt/ ...
- Codeforces Round #446
Greed #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector&g ...
- 关于KO信息
最近写大论文查到KO也是可以用于分类的一种信息. 如何使用KEGG进行通路富集http://blog.sciencenet.cn/blog-364884-779116.html kegg 数据库学习笔 ...
- javascript中变量命名冲突的问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转)shiro权限框架详解03-shiro介绍
http://blog.csdn.net/facekbook/article/details/54893740 shiro介绍 本文正式进入主题.本文将介绍如下内容: 什么是shiro 为什么需要学习 ...
- 分层、链式分析、url、联系的长度
分层.链式分析.url.联系的长度. 分层结构符合软件处理的工具链性和步骤性: 分层的每一次都是一个节点或步骤: 链式结构普遍存在于自然界,比如食物链: 联系是普遍存在的,不只是两个事物间的联系,而且 ...
- Linux 中文件名颜色所代表的属性
1. 白色:表示一般文件 2. 蓝色:表示目录 3. 绿色:表示可执行的文件或程序 4. 浅蓝色:表示链接文件 5. 黄色:表示设备文件 6. 灰色:表示其他类型文件 7. 红色:表示压缩文件或者包文 ...
- js进度条插件pace.js
主要用到themes文件夹和pace.js文件
- vue中使用mui的extra icon问题
1. 元素类名更改 2. mui下的fonts文件夹中添加mui-icons-extra.ttf文件 3. mui下的css文件中添加icons-extra.css文件 4. main.js中导入im ...