av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

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

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

1. 核心注解

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

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

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

注意:定時任務的類上還需要配合使用@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('我是定時任務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('我是定時任務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加在任務類上;

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('我是定時任務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('我是定時任務02,我執(zhí)行了' + DateUtil.formatDateByDateTime(new Date())); }}

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

3. @Scheduled的幾種用法

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

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

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 黄色成人国产 | 一区二区三区免费 | 国产伦一区二区三区视频 | 日韩中文字幕高清 | 中文字幕a√ | 小视频你懂得 | 在线免费观看毛片 | 18gay男同69亚洲网站 | 亚洲综合精品 | 精品丝袜在线 | 一区二区三区四区不卡 | av中文字幕在线观看 | 在线国产一区 | 国产精品久久久久久中文字 | 亚洲综合大片69999 | 一区二区三区观看视频 | 欧美一区二区在线观看 | 国产人成精品一区二区三 | 免费激情av | 啪啪免费网 | 成人精品啪啪欧美成 | 在线免费看毛片 | 免费黄色av | 日本亚洲欧美 | 无码日韩精品一区二区免费 | 国产一级在线视频 | 日韩欧美亚洲 | 国产精品激情在线 | 国产精品久久久久久久久久三级 | 亚洲精选一区二区 | 日韩免费一区 | 精品视频久久久久久 | www.4567 | 国产在线一区二区三区 | 国内精品久久久久久影视8 最新黄色在线观看 | 国产精品久久久久久久久 | 欧美日韩1区2区3区 欧美久久一区 | 91视频精选 | 蜜桃视频一区二区三区 | 国产精品视频在线观看 | 日韩成人影院 |