javascript
Spring @Scheduled关键字
備注:本示例中我們使用的是spring 4.2.5版本。
?
在spring的上下文文件中修改標簽:
增加Xmlns標簽:xmlns:task=http://www.springframework.org/schema/task
修改xsi:schemaLocation標簽,增加內容如下:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd
?
增加任務掃描:
<task:annotation-driven/>
?
XML如下:
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
???????? xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:tx="ttp://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="ttp://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
???????? xmlns:task="http://www.springframework.org/schema/task"
???????? xsi:schemaLocation="http://www.springframework.org/schema/beans
???????? http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
???????? http://www.springframework.org/schema/context
???????? http://www.springframework.org/schema/context/spring-context-4.2.xsd
???????? http://cxf.apache.org/jaxws
???????? http://cxf.apache.org/schemas/jaxws.xsd
???????? http://www.springframework.org/schema/tx
???????? http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
???????? http://www.springframework.org/schema/mvc
???????? http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
???????? http://www.springframework.org/schema/util
???????? http://www.springframework.org/schema/util/spring-util-4.2.xsd
???????? http://www.springframework.org/schema/aop
???????? http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
???????? http://www.springframework.org/schema/task
???????? http://www.springframework.org/schema/task/spring-task-4.2.xsd">
?
???????? <context:component-scanbase-package="com" />
???????? <task:annotation-driven />
???????? <context:annotation-config/>
</beans>
?
實現類使用注解@Component作為注釋
?
Java實例:
@Component
public class Cron {
???????? @Scheduled(cron= " 0 0 * * * ?")
???????? publicvoid runTaskPerHour() {
?????????????????? System.out.println("每小時的0分0秒執行一次: "+ new Date());
???????? }
?
???????? @Scheduled(fixedRate= 5000)
???????? publicvoid runTaskPerFiveSecond() {
?????????????????? System.out.println("立即執行,每5秒執行一次: "+ new Date());
???????? }
?
???????? @Scheduled(fixedRate= 5000, initialDelay = 10000)
???????? publicvoid runTaskPerFiveSecondDelay() {
?????????????????? System.out.println("延遲10秒執行,每5秒執行一次: "+ new Date());
???????? }
}
下方參考ITEYE
Cron介紹:
一個cron表達式有至少6個有空格分隔的時間元素。
按順序依次為
1.秒(0~59)
2.分鐘(0~59)
3.小時(0~23)
4.天(月)(0~31,但是你需要考慮你月的天數)
5.月(0~11)
6.天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
?
其中每個元素可以是一個值(如6),一個連續區間(9-12),一個間隔時間(8-18/4)(/表示每隔4小時),一個列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"這兩個元素互斥的,必須要對其中一個設置?.
"0 0 10,14,16 * * ?" 每天上午10點,下午2點,4點
"0 0/30 9-17 * * ?"?? 朝九晚五工作時間內每半小時
"0 0 12 ? * WED" 表示每個星期三中午12點
"0 0 12 * * ?" 每天中午12點觸發
"0 15 10 ? * *" 每天上午10:15觸發
"0 * 14 * * ?" 在每天下午2點到下午2:59期間的每1分鐘觸發
"0 0/5 14 * * ?" 在每天下午2點到下午2:55期間的每5分鐘觸發
"0 0/5 14,18 * * ?" 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發
"0 0-5 14 * * ?" 在每天下午2點到下午2:05期間的每1分鐘觸發
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15觸發
"0 15 10 15 * ?" 每月15日上午10:15觸發
"0 15 10 L * ?" 每月最后一日的上午10:15觸發
轉載于:https://blog.51cto.com/3418950/1891447
總結
以上是生活随笔為你收集整理的Spring @Scheduled关键字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 开机自启动脚本
- 下一篇: PYTHON黑帽编程 4.1 SNIFF