Spring整合TimerTask实现定时任务调度

2025-05-29 0 39

一. 前言

最近在公司的项目中用到了定时任务, 本篇博文将会对TimerTask定时任务进行总结, 其实TimerTask在实际项目中用的不多,
因为它不能在指定时间运行, 只能让程序按照某一个频度运行.

二. TimerTask

JDK中Timer是一个定时器类, 它可以为指定的定时任务进行配置.
JDK中TimerTask是一个定时任务类, 该类实现了Runnable接口, 是一个抽象类, 我们可以继承这个类, 实现定时任务.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
/**

* 继承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. 定时调度业务类:

?

1

2

3

4

5

6

7

8

9

10

11
/**

* 定时调度业务类

*/

public class TaskService extends TimerTask {

private int count = 1;

public void run() {

System.out.println("第" + count + "次执行定时任务");

count++;

}

}

3. 核心配置:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
<?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. 测试类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
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);

}

}

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring整合TimerTask实现定时任务调度 https://www.kuaiidc.com/119461.html

相关文章

发表评论
暂无评论