欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、基于注解

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

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 //開啟定時任務(wù)
public class ScheduleTask {
    @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式
    public void testScheduleTask() {
        System.out.println("執(zhí)行定時任務(wù)" + LocalDateTime.now());
    }
}

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

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

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

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

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

二、基于接口

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

1. 首先在數(shù)據(jù)庫中創(chuàng)建一張用來記錄定時任務(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, '定時任務(wù)1', '0/6 * * * * ?')

2. 在項目中引入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中進行連接數(shù)據(jù)庫相應(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表達式的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. 實現(xiàn)定時任務(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 //開啟定時任務(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表達式
                    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("基于接口的定時任務(wù)");
    }
}

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

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

但上面的方法定義的定時任務(wù)會有個問題,就是如果我一個定時任務(wù)里面執(zhí)行了復(fù)雜邏輯,導致本身執(zhí)行花的時間就已經(jīng)超過了定時任務(wù)間隔的時間怎么辦呢?這時候定時任務(wù)的執(zhí)行就會出現(xià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 //開啟定時任務(wù)
public class ScheduleTask {
    @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式
    public void testScheduleTask1() throws InterruptedException {
        System.out.println("執(zhí)行定時任務(wù)1 " + LocalDateTime.now());
        Thread.sleep(10 * 1000);
    }
    @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式
    public void testScheduleTask2() {
        System.out.println("執(zhí)行定時任務(wù)2 " + LocalDateTime.now());
    }
}

可以看到兩個任務(wù)的執(zhí)行時間都被影響了,和我們設(shè)置的5秒不對應(yīng)。此時就可以使用多線程定時任務(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 //開啟定時任務(wù)
@EnableAsync //開啟多線程
public class ScheduleTask {
    @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式
    @Async
    public void testScheduleTask1() throws InterruptedException {
        System.out.println("執(zhí)行定時任務(wù)1 " + LocalDateTime.now());
        Thread.sleep(10 * 1000);
    }
    @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式
    @Async
    public void testScheduleTask2() {
        System.out.println("執(zhí)行定時任務(wù)2 " + LocalDateTime.now());
    }
}

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

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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評論