spring3.0设置定时任务
来源:互联网 发布:jsp人事管理系统源码 编辑:程序博客网 时间:2024/05/13 19:01
今天做个小需求,需要用到定时器。
就把以前写过的配置文件模板直接复制过来,又顺手点进去看了一下源码,发现TimerFactoryBean、ScheduledTimerTask都已经被标记成@Deprecated了
那肯定就不乐意用了,就上网找了找spring3.0之后的新用法,果然是有变化,而且比以前简单了很多,在这里记录一下
我记得以前那种做法,业务类是要继承自TimerTask才行的,现在就不用了,是一个pojo就可以
然后配置文件也更简单
就把以前写过的配置文件模板直接复制过来,又顺手点进去看了一下源码,发现TimerFactoryBean、ScheduledTimerTask都已经被标记成@Deprecated了
- @Deprecated
- public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean
@Deprecatedpublic class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean
- @Deprecated
- public class ScheduledTimerTask
@Deprecatedpublic class ScheduledTimerTask
那肯定就不乐意用了,就上网找了找spring3.0之后的新用法,果然是有变化,而且比以前简单了很多,在这里记录一下
我记得以前那种做法,业务类是要继承自TimerTask才行的,现在就不用了,是一个pojo就可以
- public class TestService {
- private Logger logger = LoggerFactory.getLogger(TestService.class);
- public void sayHello() {
- System.out.println("hello world");
- }
- public void sayBye() {
- System.out.println("bye world");
- }
- }
public class TestService {private Logger logger = LoggerFactory.getLogger(TestService.class);public void sayHello() {System.out.println("hello world");}public void sayBye() {System.out.println("bye world");}}
然后配置文件也更简单
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <bean id="testService" class="com.xxx.spring.business.TestService" />
- <task:scheduled-tasks>
- <task:scheduled ref="testService" method="sayHello" cron="3/11 * * * * ?" />
- <task:scheduled ref="testService" method="sayBye" cron="7/13 * * * * ?" />
- </task:scheduled-tasks>
- </beans>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><bean id="testService" class="com.xxx.spring.business.TestService" /><task:scheduled-tasks><task:scheduled ref="testService" method="sayHello" cron="3/11 * * * * ?" /><task:scheduled ref="testService" method="sayBye" cron="7/13 * * * * ?" /></task:scheduled-tasks></beans>
只要用一个新增的<task:scheduled-tasks>就可以了
就是有一点要注意一下,新的时间配置,是类似于cron的语法,比以前强大很多。
不过我只用到了第一个参数:3/11,表示延迟3秒启动,间隔11秒;7/13表示延迟7秒启动,间隔13秒
转自:http://www.iteye.com/topic/1125517
- spring3.0设置定时任务
- spring3.0设置定时任务
- spring3.0定时任务
- spring3.0定时任务
- spring3.0定时任务
- spring3.0定时任务
- Spring3 定时任务
- Spring3注解配置定时任务
- BAE3.0定时任务设置
- Spring3.0与Quartz的整合实现定时任务调度
- Spring3.0与Quartz的整合实现定时任务调度
- spring3.0定时任务 (一) 简单示例
- Spring3.0与Quartz的整合实现定时任务调度
- Spring3.0与Quartz的整合实现定时任务调度
- spring3.0注解定时任务配置及说明
- Spring3.0和3.1新定时任务节点
- spring3.0注解定时任务配置及说明
- Spring3.0与Quartz的整合实现定时任务调度
- java字节流与字符流
- poj3026 Borg Maze
- 关于CSS样式优先级
- Android实现通用的ActivityGroup
- Camera Skype本地图像无法传送原因一例
- spring3.0设置定时任务
- spring之启用注解所需配置
- SQL笔记
- App层continuousFocus功能分析
- 让你的Android应用支持转移到SD卡
- 常用正则表达式
- 正则基础
- Peekmessage和Getmessage的区别
- windows批处理