午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

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

瀏覽:46日期: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)測試,當有多個包含定時任務(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)文章:
主站蜘蛛池模板: 四虎成人在线观看 | 天天爽天天操 | 亚洲午夜一区 | 日韩精品亚洲精品 | 国产精品免费在线视频 | 99re只有精品 | 亚洲综合色网 | 国产经典av | 成年在线视频 | 成人午夜大片 | 亚洲小视频在线观看 | 快色在线 | 久久久久免费看 | 亚洲国产一区二区三区在线观看 | 国产www免费观看 | 久热免费在线 | 欧美日韩高清一区二区三区 | 农村老妇性真猛 | 在线观看xxxx | 成人午夜久久 | 国产精品亚洲一区二区三区 | 麻豆免费av| 黄色小视频在线播放 | 手机看片日韩日韩 | 一本一道精品欧美中文字幕 | 国产精选一区二区 | 久久久不卡 | 久久在线精品视频 | 国产一级片毛片 | 国产无精乱码一区二区三区 | 麻豆久久久久久久 | av天天看| 亚洲天堂2024 | 黄色小视频免费看 | jizz成熟丰满日本少妇 | 一区二区视频在线 | 女人洗澡一级特黄毛片 | 成人免费在线视频观看 | 国产精品二区三区 | 麻豆精品久久 | 一二三区在线视频 |