spring3.0设置定时任务
生活随笔
收集整理的這篇文章主要介紹了
spring3.0设置定时任务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天做個小需求,需要用到定時器。
就把以前寫過的配置文件模板直接復(fù)制過來,又順手點進去看了一下源碼,發(fā)現(xiàn)TimerFactoryBean、ScheduledTimerTask都已經(jīng)被標(biāo)記成@Deprecated了
Java代碼 ?@Deprecated?? public?class?TimerFactoryBean?implements?FactoryBean<Timer>,?BeanNameAware,?InitializingBean,?DisposableBean??
@Deprecated
public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean
Java代碼 ?@Deprecated?? public?class?ScheduledTimerTask??
@Deprecated
public class ScheduledTimerTask
那肯定就不樂意用了,就上網(wǎng)找了找spring3.0之后的新用法,果然是有變化,而且比以前簡單了很多,在這里記錄一下
我記得以前那種做法,業(yè)務(wù)類是要繼承自TimerTask才行的,現(xiàn)在就不用了,是一個pojo就可以
Java代碼 ?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");}}
然后配置文件也更簡單
Xml代碼 ?<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.xsdhttp://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>
就把以前寫過的配置文件模板直接復(fù)制過來,又順手點進去看了一下源碼,發(fā)現(xiàn)TimerFactoryBean、ScheduledTimerTask都已經(jīng)被標(biāo)記成@Deprecated了
Java代碼 ?
Java代碼 ?
那肯定就不樂意用了,就上網(wǎng)找了找spring3.0之后的新用法,果然是有變化,而且比以前簡單了很多,在這里記錄一下
我記得以前那種做法,業(yè)務(wù)類是要繼承自TimerTask才行的,現(xiàn)在就不用了,是一個pojo就可以
Java代碼 ?
然后配置文件也更簡單
Xml代碼 ?
只要用一個新增的<task:scheduled-tasks>就可以了
就是有一點要注意一下,新的時間配置,是類似于cron的語法,比以前強大很多。
不過我只用到了第一個參數(shù):3/11,表示延遲3秒啟動,間隔11秒;7/13表示延遲7秒啟動,間隔13秒
?
轉(zhuǎn)自:http://www.iteye.com/topic/1125517
總結(jié)
以上是生活随笔為你收集整理的spring3.0设置定时任务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用EXE4J将JAR包转换为EXE文件
- 下一篇: 一篇很全面的freemarker教程