国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Springboot非分布式定時任務(wù)實現(xiàn)代碼

瀏覽:8日期:2023-04-07 16:01:06

1. 核心注解

在springboot項目中我們可以很方便地使用spring自己的注解@Scheduled和@EnableScheduling配合來實現(xiàn)便捷開發(fā)定時任務(wù)。

@EnableScheduling注解的作用是發(fā)現(xiàn)注解@Scheduled的任務(wù)并后臺執(zhí)行,此注解可以加到啟動類上也可以加到執(zhí)行調(diào)度任務(wù)類上。

經(jīng)測試,當(dāng)有多個包含定時任務(wù)的類時,@EnableScheduling注解加在其中一個類上就可以保證所有定時任務(wù)的成功實現(xiàn)。

注意:定時任務(wù)的類上還需要配合使用@Configuration或@Component注解,這兩個注解都可以。

2. 實例代碼

2.1 @EnableScheduling加在啟動類上;

import com.my.common.util.DateUtil;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.util.Date;/** * @description: * @author: Karl * @date: 2020/10/10 */@Componentpublic class TestSchedule01 { @Scheduled(cron = '0 * * * * ? ') public void test() { System.out.println('我是定時任務(wù)01,我執(zhí)行了' + DateUtil.formatDateByDateTime(new Date())); }}

import com.my.common.util.DateUtil;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.util.Date;/** * @description: * @author: Karl * @date: 2020/10/10 */@Configurationpublic class TestSchedule02 { @Scheduled(cron = '1 * * * * ? ') public void test() { System.out.println('我是定時任務(wù)02,我執(zhí)行了' + DateUtil.formatDateByDateTime(new Date())); }}

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}

2.1 @EnableScheduling加在任務(wù)類上;

import com.my.common.util.DateUtil;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.util.Date;/** * @description: * @author: Karl * @date: 2020/10/10 */@Component@EnableSchedulingpublic class TestSchedule01 { @Scheduled(cron = '0 * * * * ? ') public void test() { System.out.println('我是定時任務(wù)01,我執(zhí)行了' + DateUtil.formatDateByDateTime(new Date())); }}

import com.my.common.util.DateUtil;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.util.Date;/** * @description: * @author: Karl * @date: 2020/10/10 */@Configurationpublic class TestSchedule02 { @Scheduled(cron = '1 * * * * ? ') public void test() { System.out.println('我是定時任務(wù)02,我執(zhí)行了' + DateUtil.formatDateByDateTime(new Date())); }}

注意:只需要在其中一個任務(wù)類上加上@EnableScheduling注解,所有的定時任務(wù)就都可以正常運行。

3. @Scheduled的幾種用法

@Scheduled這個注解支持3種定時方式,即:cron、fixedRate和fixedDelay

cron:是以表達式的形式來表示時間,最常見;

fixedRate:表示Scheduled隔多長時間調(diào)用一次,不管任務(wù)是否執(zhí)行完;

fixedDelay:表示該任務(wù)執(zhí)行完后隔多長時間再調(diào)用;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品视频在线观看免费 | 99精品小视频 | 亚洲免费久久 | 国产美女一区二区 | 亚洲 欧美 都市 自拍 在线 | 欧美精品国产一区二区三区 | 91成人在线免费观看 | 男人天堂国产 | 国产农村乱子伦精品视频 | 成人免费xxxxx在线视频 | 亚洲国产精品免费在线观看 | 黄色网址网站 | 日韩在线第一区 | 欧美色视频在线观看 | 男人的天堂视频在线 | 日本在线视频免费看 | 在线高清免费爱做网 | 国产合集91合集久久日 | 成人在线高清 | 免费看a网站 | 欧美毛片在线 | 欧美性另类69xxxx极品 | 成人黄网大全在线观看 | a毛片免费看 | 久久久久国产精品免费网站 | 国产盗摄精品一区二区三区 | 99久久99久久久精品久久 | 国产成人刺激视频在线观看 | 美国一级免费毛片 | 欧美九九视频 | 日本免费的一级绿象 | 久久久久久中文字幕 | 国产精品视频免费播放 | 国产亚洲精品成人久久网站 | 亚洲精品一区二区三区在线观看 | 涩里番资源网站在线观看 | 一级片一区| 日韩欧美国产精品 | 亚洲性免费 | 亚洲综合国产一区在线 | 国产永久免费高清动作片www |