日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring @Scheduled关键字

發布時間:2025/6/15 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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("每小時的00秒執行一次: "+ 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 SUNMONTUEWEDTHUFRISAT

?

其中每個元素可以是一個值(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:102: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关键字的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。