久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

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

1. 核心注解

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

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

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

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

2. 實(shí)例代碼

2.1 @EnableScheduling加在啟動(dòng)類(lèi)上;

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('我是定時(shí)任務(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('我是定時(shí)任務(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ù)類(lèi)上;

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

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

3. @Scheduled的幾種用法

@Scheduled這個(gè)注解支持3種定時(shí)方式,即:cron、fixedRate和fixedDelay

cron:是以表達(dá)式的形式來(lái)表示時(shí)間,最常見(jiàn);

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

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 怀远县| 安岳县| 筠连县| 牡丹江市| 开平市| 津南区| 明溪县| 渑池县| 安乡县| 固镇县| 乌拉特中旗| 岱山县| 阜新市| 龙井市| 阿拉善左旗| 思南县| 张家口市| 内丘县| 海晏县| 故城县| 沙田区| 连云港市| 波密县| 陈巴尔虎旗| 马关县| 宣武区| 广丰县| 阿坝县| 镇远县| 宜丰县| 德保县| 贞丰县| 广汉市| 宜兰市| 中阳县| 北辰区| 若羌县| 大邑县| 英山县| 扬中市| 黄浦区|