一. 前言

近期在公司的项目中用到了定时任务, 本篇博文将会对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);
}
}
}
}

最新文章

  1. 从c#角度看万能密码SQL注入漏洞
  2. 代码的坏味道(6)——Switch声明(Switch Statements)
  3. CSS选择器中类和ID选择器的区别
  4. centos下在线安装mysql
  5. 安装MySQLdb
  6. [转]spring 注入静态变量
  7. 关于post和get传递参数的区别
  8. 精品教程--IOS零基础开发环境搭建
  9. java mail 使用 gmail smtp 发送邮件
  10. C# 获取当前年份的周期,周期所在日期范围
  11. Python连接MySQL数据库的多种方式
  12. 第一篇 - bsp抓取python中文开发者社区中的所有高级教程
  13. drop all database objects
  14. Microsoft Dynamics CRM 2011 如何导入组织
  15. Sansa组件
  16. SELinux 入门【转】
  17. 162. Find Peak Element(二分查找 )
  18. Linux安装apache服务
  19. 微信群的id
  20. threading.local()方法;线程池

热门文章

  1. GStreamer基础教程02 - 基本概念
  2. BZOJ 2288 贪心 +链表
  3. 用命令行在本地创建一个库并上传到Github
  4. android黑科技系列——微信抢红包插件原理解析和开发实现
  5. 【Oracle】搭建DG(DataGuard)
  6. 2nd
  7. Cocos2d-x-3.6学习笔记第一天
  8. DeepMind用ReinforcementLearning玩游戏
  9. 模拟一个简单的基于tcp的远程关机程序(转)
  10. 企业级任务调度框架Quartz(9) Quartz之作业触发器Trigger