在Spring中使用JDK定时器实现调度任务
在Spring中使用JDK定时器实现调度任务
作者:chszs,转载需注明。博客主页: http://blog.csdn.net/chszs
本文探讨Spring如何集成JDK的Timer定时器,实现计划执行任务。
有时候,需要执行一些无用户交互的程序,就像在指定的时间间隔后台运行进程那样。比如,杀毒软件可以每隔2天就在后台运行一次。又比如某些程序每天都要连接一次服务器,查看有没有更新。
本文探讨Spring如何集成JDK的Timer定时器,实现计划执行任务。
一、Spring框架集成JDK的Timer
JDK的Timer任务对象提供了在指定时间执行任何任务的功能。我们来看下面的例子:
假设我们想写一个服务,此服务周期性的检查互联网连接,并用日志记录连接的状态。让我们假定此服务是全天候运行的,且每隔30秒执行一次。
所需要的JAR包如下:
log4j-1.2.13.jar
commons-logging-1.1.1.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
二、写服务类
下面的类实现了互联网连接检查。
Listing 1: CheckInternetConnectionService.java
package com.chszs;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;public class CheckInternetConnectionService {public void checkConnection(){if(doCheck()){System.out.println(new Date() + "Internet connection available");}else{System.out.println(new Date() + "Internet connection not available");}}private boolean doCheck(){URL urlObject = null;URLConnection urlConnection = null;try{urlObject = new URL("http://www.baidu.com");urlConnection = urlObject.openConnection();urlConnection.getContent();return true;}catch(Exception e){return false;}}
}
上面的代码很简单,doCheck()方法检查互联网连接是否有效。
三、封装定时器任务服务
下面我们写一个服务,实现定时任务。代码如下:
Listing 2: CheckInternetConnectionWithTimerTask
package com.chszs;
import java.util.TimerTask;public class CheckInternetConnectionWithTimerTask extends TimerTask{private CheckInternetConnectionService service;public CheckInternetConnectionService getService(){return service;}public void setService(CheckInternetConnectionService service){this.service = service;}@Overridepublic void run() {service.checkConnection();}
}
此类继承了java.util.TimerTask类。
重写了run()方法,可以执行任意操作。这里是调用互联网连接检查。
注意定时器任务依赖于连接服务对象。稍后,我们将看到怎样连线这两个对象。
四、配置
至今,我们还没有指定执行的时间间隔。Spring提供了这样的配置支持。下面我们来看看该如何配置:
Listing 3: timer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="connectionCheckService" class="com.chszs.CheckInternetConnectionService"></bean><bean id="connectionCheckTimerTask" class="com.chszs.CheckInternetConnectionWithTimerTask"><property name="service" ref="connectionCheckService" /></bean><bean id="scheduledConnectionCheckTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="delay" value="2000" /><property name="period" value="30000" /><property name="timerTask" ref="connectionCheckTimerTask" /></bean><bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref bean="scheduledConnectionCheckTimerTask" /></list></property></bean>
</beans>
以上配置文件的细节:
"connectionCheckService"这个Bean表示互联网连接服务。
"connectionCheckTimerTask"这个Bean定义了定时器任务。由于此定时器任务依赖于"connectionCheckService"这个Bean,故通过配置进行注入。
下面的代码是从Spring框架中声明定时器任务的调度对象:
<bean id="scheduledConnectionCheckTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="delay" value="2000" /><property name="period" value="30000" /><property name="timerTask" ref="connectionCheckTimerTask" /></bean>
org.springframework.scheduling.timer.ScheduledTimerTask这个类提供了对定时任务调度执行的支持。
属性delay的单位是毫秒,它指定任务执行前需要延时多少时间。2000意味着延时2秒开始执行任务。
第二个属性period的单位也是毫秒,它表示任务每隔多少时间就重复执行一次。30000这个值表示每隔30秒执行一次。
最后一个属性是timerTask,它指定实际要执行的任务。
触发调度任务是通过TimerFactoryBean进行的。它可以指定待调度的任务对象列表,尽管这里只有1个待调度的任务对象。
<bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref bean="scheduledConnectionCheckTimerTask" /></list></property></bean>
五、客户端
客户端程序会载入应用程序的上下文。一旦上下文被载入,服务对象、定时器任务对象、调度的定时器任务对象都会被载入并连线。下面我们继续介绍触发器Bean是如何触发定时器任务的执行,互联网连接在每隔30秒运行一次。
Listing 4: Client.java
package com.chszs;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Client {public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext("timer.xml");}
}
运行Client.java,可以看到每隔30秒定时器任务就调度执行一次。
执行结果如下:
Sun Aug 11 21:08:26 CST 2013Internet connection available
Sun Aug 11 21:08:56 CST 2013Internet connection available
Sun Aug 11 21:09:26 CST 2013Internet connection available
Sun Aug 11 21:09:56 CST 2013Internet connection available
转载于:https://www.cnblogs.com/pangblog/p/3253667.html
在Spring中使用JDK定时器实现调度任务相关推荐
- spring中配置quartz定时器
spring中配置quartz定时器 最近项目中用到定时器,项目用的spring所以想在spring中配置一下定时器,看到网上用quartz的比较多,所以就搜了一下.参考:http://blog.cs ...
- spring中的jdk动态代理(代码步骤)
UserDao.java接口: package com.liu.jdk;public interface UserDao {public void addUser();public void dele ...
- Spring中使用Schedule调度
在spring中两种办法使用调度,以下使用是在spring4.0中. 一.基于application配置文件,配置入下: 1 <bean id="jobDetail" cla ...
- Spring-JDK Timer 以及在Spring(4.0以下)中使用JDK Timer
概述 Timer 和 TimerTask 抽象类TimerTask Timer Timer构造函数及方法 示例 Spring对Java Timer的支持 Spring40已经不支持了推荐使用Quart ...
- Spring中Quartz调度器的使用
一.Quartz的特点 * 按作业类的继承方式来分,主要有以下两种: 1.作业类继承org.springframework.scheduling.quartz.QuartzJobBean类的方式 2. ...
- java怎么加定时器,Spring中定时器实现
在一些工作需要使用到定时器,Spring很好的集成了定时器的功能! 在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻 ...
- java定时调度main方法_java相关:Spring中实现定时调度的几种方法
java相关:Spring中实现定时调度的几种方法 发布于 2020-11-28| 复制链接 本篇文章主要介绍了Spring中实现定时调度示例,可以在无人值守的时候系统可以在某一时刻执行某些特定的功能 ...
- JDK和Spring中的设计模式
JDK中的设计模式(17) 创建型 1)工厂方法 Collection.iterator() 由具体的聚集类来确定使用哪一个Iterator 2)单例模式 Runtime.getRuntime() 3 ...
- spring中定时器的使用
在很多实际的web应用中,都有需要定时实现的服务,如每天12点推送个新闻,每隔一个小时提醒用户休息一下眼睛,隔一段时间检测用户是否离线等等. spring框架提供了对定时器的支持,通过配置文件就可以很 ...
最新文章
- 三个能够造成重大损失的低技术含量攻击
- web移动端_移动端的轮播
- win7安装mysql-5.7.18
- RTD-D项目总结(MATLAB)
- 算法中的算子是什么意思,图像处理一样理解
- 深度学习精度提升 3 个小妙招:模型集成、知识蒸馏、自蒸馏
- 【kafka】kafka consumer offset lag获取的三者方式
- go 是常驻内存吗_图解 Go 内存分配器
- MySQL5.7.11免安装版的安装和配置:解决MYSQL 服务无法启动问题
- 【文文殿下】浅谈KMP算法next数组与循环节的关系
- 苹方字体 android,iOS 9“苹方”字体像安卓被吐槽
- PN5180射频识别芯片学习笔记
- 代码检查工具!从 TSLint 到 ESLint
- JavaScript 使用js修改页面元素
- 使用OpenWrt创建子网作为二级路由
- 流星蝴蝶剑服务器状态,流星蝴蝶剑什么是数据互通 哪些服务器会进行互通
- 软件加密系统Themida应用程序保护指南(七):外挂插件
- Swift QQ授权登录 坑集
- php获取linux服务器CPU、内存、硬盘使用率的实现代码
- ss7 的主叫地址性质