javascript
Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)
在spring 中的新引入的task?命名空間。可以部分取代?quartz 功能,配置和API更加簡(jiǎn)單,并且支持注解方式。
?
第一步:
??????? 在Spring的相關(guān)配置文件中(applicationContext.xml或者是{project_name}_servelt.xml或者是獨(dú)立的配置文件如XXX_quartz.xml)中配置并開(kāi)啟Spring Schedule Task.注意其中高亮的部分是必須的。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xmlns:p="http://www.springframework.org/schema/p" 9 xmlns:task="http://www.springframework.org/schema/task" 10 xsi:schemaLocation=" 11 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 12 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 15 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 16 http://www.springframework.org/schema/task 17 http://www.springframework.org/schema/task/spring-task-3.0.xsd 18 "> 19 <mvc:annotation-driven /> 20 <context:component-scan base-package="com.mytools.validator.engine" /> 21 22 <!-- 啟動(dòng)定時(shí)器 --> 23 <task:annotation-driven/> 24 </beans>?
?
?
第二步:
??????? 可以在類中的需要定時(shí)執(zhí)行的方法下指定如下Annotation
1 @Scheduled(cron="0 33/3 * * * ?") //每小時(shí)的33分鐘開(kāi)始執(zhí)行,每3分鐘執(zhí)行1次 2 public void start() throws ServletException { 3 validate(); 4 }?
備注:其實(shí)@Scheduled中可以指定如下3種時(shí)間表達(dá)式:
(1)fixedRate:每隔多少毫秒執(zhí)行一次該方法。如:
1 @Scheduled(fixedRate=2000) // 每隔2秒執(zhí)行一次 2 public void scheduleMethod(){ 3 System.out.println("Hello world..."); 4 }?
(2)fixedDelay:當(dāng)一次方法執(zhí)行完畢之后,延遲多少毫秒再執(zhí)行該方法。
(3)cron:詳細(xì)配置了該方法在什么時(shí)候執(zhí)行。cron值是一個(gè)cron表達(dá)式。如:
?
1 @Scheduled(cron="0 0 0 * * SAT") 2 public voidarchiveOldSpittles() { 3 // ... 4 }?
?到指定時(shí)間后,任務(wù)總是執(zhí)行2次的解決方案:
這是因?yàn)槲覀兒苋菀自谝粋€(gè)基于Spring的Web工程中啟動(dòng)2個(gè)定時(shí)線程:
第一次:web容器啟動(dòng)的時(shí)候,讀取applicationContext.xml(或者別的Spring核心配置文件)文件時(shí),會(huì)加載一次。
第二次:Spring本身會(huì)加載applicationContext.xml(或者別的Spring核心配置文件)一次。
解決方案:將你的Task的相關(guān)配置獨(dú)立出來(lái)并在web.xml中通過(guò)context-param加載。而不是通過(guò)spring加載。
1) 獨(dú)立出Spring-Task,如新命名一個(gè)文件名叫cms_quartz.xml
2)??? 在web.xml中去加載該文件:
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>/WEB-INF/cms-servlet.xml, classpath:cms-quartz.xml</param-value> 4 </context-param>?
注:出自 http://www.tuicool.com/articles/jmU7bq
?
轉(zhuǎn)載于:https://www.cnblogs.com/kzhan/p/5742523.html
總結(jié)
以上是生活随笔為你收集整理的Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Nginx 配置从零开始
- 下一篇: 只要你想学,分分钟钟用H5教会你玩转魔方