色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

Springboot非分布式定時任務實現代碼

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

1. 核心注解

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

@EnableScheduling注解的作用是發現注解@Scheduled的任務并后臺執行,此注解可以加到啟動類上也可以加到執行調度任務類上。

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

注意:定時任務的類上還需要配合使用@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,我執行了' + 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,我執行了' + 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,我執行了' + 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,我執行了' + DateUtil.formatDateByDateTime(new Date())); }}

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

3. @Scheduled的幾種用法

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

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

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

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

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 日韩一区二区三区精品 | 亚洲欧美视频一级 | 亚洲国产欧美精品一区二区三区 | 日本一区二区三区四区五区 | 久久国产精品1区2区3区网页 | 亚洲成人网页 | 免费一级欧美大片久久网 | 亚洲精品久久久久久久网站 | 欧美在线视频免费 | 日韩在线欧美在线 | 亚洲男人的天堂在线观看 | 99免费精品 | 国产v综合v亚洲欧美大另类 | 兔子先生节目在线观看免费 | 日韩高清不卡在线 | 久久的精品99精品66 | 怡红院男人的天堂 | 在线亚洲精品国产波多野结衣 | 性感美女一级片 | 国产欧美日韩图片一区二区 | 成年美女黄网站小视频 | 男人天堂男人天堂 | 国产成人亚洲精品无广告 | 久久亚洲国产精品五月天 | 欧美在线精品一区二区三区 | 精品视频一区二区 | 免费五级在线观看日本片 | 白白在线观看永久免费视频 | 第一色网站 | 日韩免费一级a毛片在线播放一级 | 国产精品久久免费观看 | 夜色福利久久久久久777777 | 国产成人精品系列在线观看 | 欧美成人综合 | 第四色成人网 | 姐姐真漂亮在线视频中文版 | 国产欧美视频综合二区 | 欧美在线一二三区 | 一区高清 | 久久国产一区二区 | 久久国产欧美 |