spring注解@Scheduled中fixedDelay、fixedRate和cron表達式的區別


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/task 
                            http://www.springframework.org/schema/task/spring-task-3.0.xsd 
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<context:component-scan base-package="com.test.*"/>
	<!--需要掃描的包-->
	<task:annotation-driven/> <!-- 這句是重點 定時器開關-->
</beans>

 

添加task定時任務的xml文件,配置在spring文件中

@Scheduled注解可以控制方法定時執行,其中有三個參數可選擇:

1、fixedDelay控制方法執行的間隔時間,是以上一次方法執行完開始算起,如上一次方法執行阻塞住了,那么直到上一次執行完,並間隔給定的時間后,執行下一次。

2、fixedRate是按照一定的速率執行,是從上一次方法執行開始的時間算起,如果上一次方法阻塞住了,下一次也是不會執行,但是在阻塞這段時間內累計應該執行的次數,當不再阻塞時,一下子把這些全部執行掉,而后再按照固定速率繼續執行。

3、cron表達式可以定制化執行任務,但是執行的方式是與fixedDelay相近的,也是會按照上一次方法結束時間開始算起。

 

具體使用方法參看使用Spring的@Scheduled實現定時任務
---------------------
作者:依鵬
來源:CSDN
原文:https://blog.csdn.net/m0_37240709/article/details/78499542
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM