亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式

 更新時(shí)間:2023年05月24日 08:51:46   作者:JK凱  
定時(shí)任務(wù)在我們項(xiàng)目開(kāi)發(fā)中也是很重要的,對(duì)于某些場(chǎng)景必須要用定時(shí)任務(wù)?,如定時(shí)發(fā)送郵件啊,定時(shí)統(tǒng)計(jì)數(shù)據(jù)等,這篇文章主要講講項(xiàng)目中實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式,需要的朋友可以參考下

一、基于注解

這種方式很簡(jiǎn)單,主要就是先@EnableScheduling開(kāi)啟定時(shí)任務(wù)功能,然后在相應(yīng)的方法上添加@Scheduled()中間寫上相應(yīng)的cron表達(dá)式即可。示例如下:

schedule.ScheduleTask:

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling //開(kāi)啟定時(shí)任務(wù)
public class ScheduleTask {
    @Scheduled(cron = "0/5 * * * * ?") //定時(shí)任務(wù)注解+cron表達(dá)式
    public void testScheduleTask() {
        System.out.println("執(zhí)行定時(shí)任務(wù)" + LocalDateTime.now());
    }
}

Cron表達(dá)式參數(shù)參考:

  • 秒(0~59) 例如0/5表示每5秒
  • 分(0~59)
  • 時(shí)(0~23)
  • 日(0~31)的某天,需計(jì)算
  • 月(0~11)
  • 周幾( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)

建議:直接在線生成Cron表達(dá)式比較方便:www.matools.com/cron/

@Scheduled:除了支持靈活的參數(shù)表達(dá)式cron之外,還支持 fixedDelay,fixedRate,initialDelay 這些延時(shí)性的操作。

啟動(dòng)測(cè)試就實(shí)現(xiàn)了基本的定時(shí)任務(wù)功能,但是如果我們修改了cron表達(dá)式,需要重啟整個(gè)應(yīng)用才能生效,不是很方便。想要實(shí)現(xiàn)修改cron表達(dá)式就生效就需要用到接口的方式來(lái)實(shí)現(xiàn)定時(shí)任務(wù)。

二、基于接口

接口的方式是我們把定時(shí)任務(wù)的信息放在數(shù)據(jù)庫(kù)中,程序從數(shù)據(jù)庫(kù)去拉取定時(shí)任務(wù)的信息如cron表達(dá)式來(lái)實(shí)現(xiàn)實(shí)時(shí)修改生效等功能。

1. 首先在數(shù)據(jù)庫(kù)中創(chuàng)建一張用來(lái)記錄定時(shí)任務(wù)的表

CREATE TABLE `scheduled`?(
? `id` bigint NOT NULL AUTO_INCREMENT,
? `name` varchar(255) NULL,
? `cron` varchar(255) NULL,
? PRIMARY KEY (`id`)
)
INSERT INTO `mydb`.`scheduled` (`id`, `name`, `cron`) VALUES (1, '定時(shí)任務(wù)1', '0/6 * * * * ?')

2. 在項(xiàng)目中引入mabatis-plusmysql相應(yīng)的依賴包

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.5.3.1</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.32</version>
</dependency>

3. 在application.yml中進(jìn)行連接數(shù)據(jù)庫(kù)相應(yīng)配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8&serverTimeZone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

4. 定義查詢cron表達(dá)式的mapper

mapper.CronMapper:

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CronMapper {
    @Select("select cron from scheduled where id=#{id}")
    String getCron(Long id);
}

5. 實(shí)現(xiàn)定時(shí)任務(wù)

import com.jk.mapper.CronMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling //開(kāi)啟定時(shí)任務(wù)
public class ScheduleTask implements SchedulingConfigurer {
    @Autowired
    private CronMapper cronMapper;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //添加任務(wù)內(nèi)容
                () -> process(),
                //設(shè)置執(zhí)行的周期
                triggerContext -> {
                    //查詢cron表達(dá)式
                    String cron = cronMapper.getCron(1L);
                    if (cron.isEmpty()) {
                        System.out.println("cron is null");
                    }
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                });
    }
    private void process() {
        System.out.println("基于接口的定時(shí)任務(wù)");
    }
}

這種方式需要去實(shí)現(xiàn)SchedulingConfigurer接口并重寫configureTasks方法,然后設(shè)置任務(wù)內(nèi)容和執(zhí)行周期等,啟動(dòng)測(cè)試就實(shí)現(xiàn)了基于接口的定時(shí)任務(wù),此時(shí)我們改動(dòng)數(shù)據(jù)庫(kù)里的cron表達(dá)式也會(huì)實(shí)時(shí)生效

三、多線程定時(shí)任務(wù)

但上面的方法定義的定時(shí)任務(wù)會(huì)有個(gè)問(wèn)題,就是如果我一個(gè)定時(shí)任務(wù)里面執(zhí)行了復(fù)雜邏輯,導(dǎo)致本身執(zhí)行花的時(shí)間就已經(jīng)超過(guò)了定時(shí)任務(wù)間隔的時(shí)間怎么辦呢?這時(shí)候定時(shí)任務(wù)的執(zhí)行就會(huì)出現(xiàn)一定的問(wèn)題,具體如下,我用線程睡眠的方式模擬處理復(fù)雜邏輯

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling //開(kāi)啟定時(shí)任務(wù)
public class ScheduleTask {
    @Scheduled(cron = "0/5 * * * * ?") //定時(shí)任務(wù)注解+cron表達(dá)式
    public void testScheduleTask1() throws InterruptedException {
        System.out.println("執(zhí)行定時(shí)任務(wù)1 " + LocalDateTime.now());
        Thread.sleep(10 * 1000);
    }
    @Scheduled(cron = "0/5 * * * * ?") //定時(shí)任務(wù)注解+cron表達(dá)式
    public void testScheduleTask2() {
        System.out.println("執(zhí)行定時(shí)任務(wù)2 " + LocalDateTime.now());
    }
}

可以看到兩個(gè)任務(wù)的執(zhí)行時(shí)間都被影響了,和我們?cè)O(shè)置的5秒不對(duì)應(yīng)。此時(shí)就可以使用多線程定時(shí)任務(wù)

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling //開(kāi)啟定時(shí)任務(wù)
@EnableAsync //開(kāi)啟多線程
public class ScheduleTask {
    @Scheduled(cron = "0/5 * * * * ?") //定時(shí)任務(wù)注解+cron表達(dá)式
    @Async
    public void testScheduleTask1() throws InterruptedException {
        System.out.println("執(zhí)行定時(shí)任務(wù)1 " + LocalDateTime.now());
        Thread.sleep(10 * 1000);
    }
    @Scheduled(cron = "0/5 * * * * ?") //定時(shí)任務(wù)注解+cron表達(dá)式
    @Async
    public void testScheduleTask2() {
        System.out.println("執(zhí)行定時(shí)任務(wù)2 " + LocalDateTime.now());
    }
}

這樣多線程的定時(shí)任務(wù)就實(shí)現(xiàn)了,每個(gè)定時(shí)任務(wù)之間不會(huì)互相影響,定時(shí)任務(wù)執(zhí)行時(shí)間太長(zhǎng)也不會(huì)影響。

這就是定時(shí)任務(wù)實(shí)現(xiàn)的幾種方式,對(duì)大家有幫助的話多多點(diǎn)贊、收藏哦,感謝!

以上就是SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 定時(shí)任務(wù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring中@DependsOn注解的作用及實(shí)現(xiàn)原理解析

    Spring中@DependsOn注解的作用及實(shí)現(xiàn)原理解析

    這篇文章主要介紹了Spring中@DependsOn注解的作用及實(shí)現(xiàn)原理解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Spring的事件監(jiān)聽(tīng)機(jī)制示例詳解

    Spring的事件監(jiān)聽(tīng)機(jī)制示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring的事件監(jiān)聽(tīng)機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Java匿名內(nèi)部類原理與用法詳解

    Java匿名內(nèi)部類原理與用法詳解

    這篇文章主要介紹了Java匿名內(nèi)部類原理與用法,結(jié)合實(shí)例形式分析了Java匿名內(nèi)部類的概念、原理、應(yīng)用與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解

    Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解

    這篇文章主要介紹了Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 舉例講解Java中的多線程編程

    舉例講解Java中的多線程編程

    這篇文章主要介紹了舉例講解Java中的多線程編程,線程是Java學(xué)習(xí)中的重要知識(shí),需要的朋友可以參考下
    2015-09-09
  • Spring Security的持久化用戶和授權(quán)實(shí)現(xiàn)方式

    Spring Security的持久化用戶和授權(quán)實(shí)現(xiàn)方式

    文章介紹了如何使用JdbcUserDetailsManager實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀取用戶,并展示了如何配置SpringSecurity進(jìn)行授權(quán)管理,通過(guò)創(chuàng)建數(shù)據(jù)庫(kù)表、配置數(shù)據(jù)庫(kù)連接和修改SecurityConfig,實(shí)現(xiàn)了用戶權(quán)限的控制
    2025-02-02
  • java中catalina.home與catalina.base區(qū)別點(diǎn)整理

    java中catalina.home與catalina.base區(qū)別點(diǎn)整理

    在本篇文章里小編給大家整理的是關(guān)于java項(xiàng)目中catalina.home與catalina.base區(qū)別點(diǎn),需要的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • Java之Set?交集,差集,并集的用法

    Java之Set?交集,差集,并集的用法

    這篇文章主要介紹了Java之Set?交集,差集,并集的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Spring AOP在web應(yīng)用中的使用方法實(shí)例

    Spring AOP在web應(yīng)用中的使用方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Spring AOP在web應(yīng)用中的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java實(shí)現(xiàn)在線預(yù)覽的示例代碼(openOffice實(shí)現(xiàn))

    Java實(shí)現(xiàn)在線預(yù)覽的示例代碼(openOffice實(shí)現(xiàn))

    本篇文章主要介紹了Java實(shí)現(xiàn)在線預(yù)覽的示例代碼(openOffice實(shí)現(xiàn)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11

最新評(píng)論