大家应该都有所体会, 在很多实际的 web 应用中, 都有需要定时实现的服务, 下面这篇文章主要给大家介绍了关于 spring 中定时器的使用教程, 对大家具有一定的参考学习价值, 需要的朋友们下面来一起看看吧
前言
在很多实际的 web 应用中, 都有需要定时实现的服务, 如每天 12 点推送个新闻, 每隔一个小时提醒用户休息一下眼睛, 隔一段时间检测用户是否离线等等
spring 框架提供了对定时器的支持, 通过配置文件就可以很好的实现定时器, 只需要应用启动, 就自动启动定时器下面介绍一下具体做法
第一种, 使用 XML 配置的方法
前期工作, 配置 spring 的开发环境 (这里用到了 spring 的 web 应用包, 需要导入)
首先创建定时器的任务类, 定时器要做什么工作, 就在这里写什么方法
- package org.time;
- import java.util.TimerTask;
- public class MainTask extends TimerTask{
- @Override
- public void run() {
- System.out.println("检测用户是否掉线");
- }
- }
接着在配置文件中对定时器进行配置
- <?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="mainTask" class="org.time.MainTask">
- </bean>
- <!-- 注册定时器信息 -->
- <bean id="springTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
- <!-- 延迟 1 秒执行首次任务 -->
- <property name="delay" value="1000">
- </property>
- <!-- 每隔 2 秒执行一次任务 -->
- <property name="period" value="2000">
- </property>
- <!-- 具体执行的任务 -->
- <property name="timerTask" ref="mainTask">
- </property>
- </bean>
- <!-- 配置任务调度器工厂 -->
- <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
- <property name="scheduledTimerTasks">
- <list>
- <ref bean="springTask" />
- </list>
- </property>
- </bean>
- </beans>
最后还需要在 web.xml 中对配置信息进行注册:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- </web-app>
这样就完成了定时器的配置, 这时启动 tomcat, 观察控制台输出的结果:
第二种, 使用注解的形式
(spring 中一使用注解, 感觉就是比其他方法方便了很多, 代码减少了很多)
这里需要用到 AOP, 需要引入 AOP 类库
先看定时器的任务类:
- package org.time;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- @Component
- public class MainTask01{
- @Scheduled(cron = "0/3 * * * * ?")
- public void run() {
- System.out.println("推送消息来了");
- }
- }
@Scheduled(cron = "0/3 * * * * ?") 表示三秒推送一次
corn 可以配置各种时段任务:
字段
|
值
|
特殊表示字符
|
年
|
一般为空,1970-2099
|
, - * /
|
月
|
1-12 或者 JAN-DEC
|
, - * /
|
星期
|
1-7 或者 SUN-SAT
|
, - * ? / L C #
|
日
|
1-31
|
, - * ? / L W C
|
时
|
0-23
|
, - * /
|
分
|
0-59
|
, - * /
|
秒
|
0-59
|
, - * /
|
如: 配置每个工作日的 10:20 触发 :"0 20 10 ? * MON-FRI"
配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:task="http://www.springframework.org/schema/task"
- xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task.xsd">
- <context:annotation-config />
- <!-- spring 扫描注解的配置 -->
- <context:component-scan base-package="org.time" />
- <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
- <task:scheduler id="qbScheduler" pool-size="10"/>
- </beans>
配置文件的头部信息中比上一个引入了
- xmlns:task="http://www.springframework.org/schema/task"
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task.xsd
- <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
- <task:scheduler id="qbScheduler" pool-size="10"/>
这两句配置信息是必须要写的, 这是 spring 识别 @Scheduled 注解的关键
这这样简单的几句配置之后, 开启服务, 运行结果:
spring 中使用注解的方法完成定时器, 不需要集成其他父类定时器, 使用简单方便! 代码量少, 功能也很强大!
总结
来源: http://www.phperz.com/article/18/0316/356758.html