久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

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

瀏覽:11日期: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
相關文章:
主站蜘蛛池模板: 不卡一区| 久久久免费电影 | 亚洲国产成人久久一区二区三区 | 色婷婷av一区二区三区软件 | 欧美一区二区三区精品 | 久久亚洲天堂 | 日韩欧美在线观看视频 | 精品中文字幕在线观看 | 搞黄免费视频 | 国产精品久久久久久一区二区三区 | 国产精品久久久久久久久久久久 | 国产精品第一区 | 91在线免费看 | av一二三四| 欧美日韩一区二区三区四区 | 日韩大片免费播放 | 久久中文字幕一区 | 日韩精品视频三区 | av免费网站在线观看 | 日韩欧美国产一区二区 | 黄色成人av| 免费一看一级毛片 | 超碰伊人网 | 激情综合色综合久久综合 | 国产一在线 | 国产精品久久久久久久久大全 | 欧美国产精品一区二区 | 凹凸日日摸日日碰夜夜 | 欧美aaa视频 | 91免费国产 | 成人黄视频在线观看 | 91久久久久久 | 99久久精品免费看国产一区二区三区 | 嫩草视频网 | 精品久久久久一区二区三区 | 欧美精品一区二区三区手机在线 | 色网在线看 | 天天躁日日躁性色aⅴ电影 免费在线观看成年人视频 国产欧美精品 | 久久久久无码国产精品一区 | 亚洲视频中文字幕 | h片观看 |