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服务外,还有一类不可缺少的,那就是定时任务调度.定时任务的场景可以说非常广泛,比如某些视频网站,购买会员后,每天会给会员送成长值,每月会给会员送一些电影券 ...
随机推荐
- 利用道格拉斯·普客法(DP法)压缩矢量多边形(C++)
1.算法描述 经典的Douglas-Peucker算法(简称DP法)描述如下: (1)在曲线首尾两点A,B之间连接一条直线AB,该直线为曲线的弦: (2)得到曲线上离该直线段距离最大的点C,计算其与A ...
- 乐字节-Java8核心特性实战-接口默认方法
JAVA8已经发布很久,是自java5(2004年发布)之后Oracle发布的最重要的一个版本.其中包括语言.编译器.库.工具和JVM等诸多方面的新特性,对于国内外互联网公司来说,Java8是以后技术 ...
- springmvc-servlet.xml(springmvc-servlet.xml 配置 增强配置)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- MacType 缺字问题【转】
- PostgreSQL导出表中数据
下边的步骤详细讲述了从Postgres数据库中导出数据的方法: (1)将PostgreSQL数据库的psql工具所在的路径添加到系统的环境变量中:(2)运行cmd,在窗口中输入psql,会有提示输入口 ...
- Verilog之$sreadmemb
1 Memories Memories file format is shown below, the address is specified as @ <address> in he ...
- C# 1.将整个文件夹复制到目标文件夹中 2.将指定文件复制到指定目标文件夹中
].Items.Clear(); string filePath = Application.StartupPath; string sourcePath = Path.Combine(filePat ...
- jdbc转账操作
public class cs{ public static void main(String[] args){ try{ Connection conn=JdbcUtils.getConnectio ...
- day25-1 time,datetime模块
目录 time 为什么要有time模块,time模块有什么用 时间戳形式 格式化时间 结构化时间 各种时间格式互相转换 datetime 为什么要有datetime模块,detatime模块有什么用 ...
- K3 新单到老单关联字段的添加
新单到老单字段的添加分为两种: 一种为文本字段信息的关联,新单与老单字段的信息均为文本字段: 另一种为基础资料信息的关联,新单与老单均为基础资料字段信息. K3 WISE 11.0中存储老 ...