SpringBoot中的定時任務的同步與異步


定時任務調度功能在我們的開發中是非常常見的,隨便舉幾個例子:定時清除一些過期的數據,定時發送郵件等等,實現定時任務調度的方式也十分多樣,本篇文章主要學習各種實現定時任務調度方式的優缺點,以便為日后選擇的時候提供一定的參考。

本篇要點

  • 介紹Timer實現定時任務。

  • 介紹ScheduledExecutorService實現定時任務。

  • 介紹SpringBoot使用SpringTask實現定時任務。

  • 介紹SpringBoot使用SpringTask實現異步任務。

Timer實現定時任務

基於JDK自帶的java.util.Timer,通過調度java.util.TimeTask讓某一段程序按某一固定間隔,在某一延時之后定時執行。

缺點:

  1. 無法指定某一時間的時候執行。
  2. 存在潛在bug,Timer運行多個TimeTask時,只要其中之一沒有捕獲拋出的異常,其它任務便會自動終止運行。
public class DemoTimer {

    //延時時間
    private static final long DELAY = 3000;

    //間隔時間
    private static final long PERIOD = 5000;

    public static void main(String[] args) {
        // 定義要執行的任務
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("任務執行 --> " + LocalDateTime.now());
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, DELAY, PERIOD);
    }
}

ScheduledExecutorService實現定時任務

阿里巴巴開發規范明確規定:希望開發者使用ScheduledExecutorService代替Timer。

多線程並行處理定時任務時,Timer運行多個TimeTask時,只要其中之一沒有捕獲拋出的異常,其它任務便會自動終止運行,使用ScheduledExecutorService則沒有這個問題。

public class DemoScheduledExecutorService {

    //延時時間
    private static final long DELAY = 3000;

    //間隔時間
    private static final long PERIOD = 5000;

    public static void main(String[] args) {

        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println("任務執行 --> " + LocalDateTime.now());
            }
        };

        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(task, DELAY, PERIOD, TimeUnit.MILLISECONDS);

    }
}

SpringBoot使用Spring Task實現定時任務

自動配置實現原理

Spring為我們提供了異步執行任務調度的方式,提供TaskExecutor,TaskScheduler接口,而SpringBoot的自動配置類org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration為我們默認注入了他們的實現:ThreadPoolTaskScheduler,本質上是ScheduledExecutorService 的封裝,增強在調度時間上的功能。

@ConditionalOnClass(ThreadPoolTaskScheduler.class)
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(TaskSchedulingProperties.class)
@AutoConfigureAfter(TaskExecutionAutoConfiguration.class)
public class TaskSchedulingAutoConfiguration {

	@Bean
	@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
	@ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class, ScheduledExecutorService.class })
	public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
		return builder.build();
	}

	@Bean
	@ConditionalOnMissingBean
	public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
			ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
		TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
		builder = builder.poolSize(properties.getPool().getSize());
		Shutdown shutdown = properties.getShutdown();
		builder = builder.awaitTermination(shutdown.isAwaitTermination());
		builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
		builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
		builder = builder.customizers(taskSchedulerCustomizers);
		return builder;
	}

}

新建工程,引入依賴

Spring Task是Spring Framework中的模塊,我們只需引入spring-boot-starter依賴就可以了。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

編寫配置類@EnableScheduling

@Configuration
@EnableScheduling
public class ScheduleConfig {

}
  • @Configuration表明這是個配置類。
  • @EnableScheduling表明啟用Spring的定時任務調度功能。

定義定時任務@Scheduled

@Component
@Slf4j
public class DemoTask {

    private final AtomicInteger counts = new AtomicInteger();

    @Scheduled(cron = "0/5 * * * * *")
    public void execute() {
        log.info("[定時任務第 {} 次執行]", counts.incrementAndGet());
    }
}
  • @Component表明該類需要被掃描,以便於Spring容器管理。
  • @Scheduled標注需要調度執行的方法,定義執行規則,其必須指定cronfixedDelayfixedRate三個屬性其中一個。
    • cron:定義Spring cron表達式,網上有在線cron生成器,可以對照着編寫符合需求的定時任務。
    • fixedDelay :固定執行間隔,單位:毫秒。注意,以調用完成時刻為開始計時時間。
    • fixedRate :固定執行間隔,單位:毫秒。注意,以調用開始時刻為開始計時時間。

主啟動類

@SpringBootApplication
public class SpringBootTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTaskApplication.class, args);
    }
}

定義配置文件

Spring Task 調度任務的配置,對應 TaskSchedulingProperties 配置類。SpringBoot允許我們在yml或properties定制這些外部化配置,如果不配置也是沒有關系的,自動配置已經給你一套默認的值了。

spring:
  task:
    scheduling:
      thread-name-prefix: summerday- # 線程池的線程名的前綴。默認為 scheduling- ,建議根據自己應用來設置
      pool:
        size: 10 # 線程池大小。默認為 1 ,根據自己應用來設置
      shutdown:
        await-termination: true # 應用關閉時,是否等待定時任務執行完成。默認為 false ,建議設置為 true
        await-termination-period: 60 # 等待任務完成的最大時長,單位為秒。默認為 0 ,根據自己應用來設置

啟動項目測試

# 初始化一個 ThreadPoolTaskScheduler 任務調度器
2020-11-30 23:04:51.886  INFO 10936 --- [  restartedMain] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
# 每5s執行一次定時任務
2020-11-30 23:04:55.002  INFO 10936 --- [    summerday-1] com.hyh.task.DemoTask                    : [定時任務第 1 次執行]
2020-11-30 23:05:00.002  INFO 10936 --- [    summerday-1] com.hyh.task.DemoTask                    : [定時任務第 2 次執行]
2020-11-30 23:05:05.002  INFO 10936 --- [    summerday-2] com.hyh.task.DemoTask                    : [定時任務第 3 次執行]
2020-11-30 23:05:10.001  INFO 10936 --- [    summerday-1] com.hyh.task.DemoTask                    : [定時任務第 4 次執行]
2020-11-30 23:05:15.002  INFO 10936 --- [    summerday-3] com.hyh.task.DemoTask                    : [定時任務第 5 次執行]

SpringTask異步任務

SpringTask除了@Scheduled、@EnableScheduling同步定時任務之外,還有@Async、@EnableAsync 開啟異步的定時任務調度。

SpringBoot自動配置類對異步的支持:org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration

@Async注解添加

    @Async
    @Scheduled(cron = "0/1 * * * * *")
    public void asyncTask() {
        sleep();
        System.out.println(Thread.currentThread().getName() + " async-task 執行,當前時間: " + LocalDateTime.now());
    }

@EnableAsync注解添加

@Configuration
@EnableScheduling  // 同步
@EnableAsync // 異步
public class ScheduleConfig {

}

配置文件

spring:
  task:
    # Spring 執行器配置,對應 TaskExecutionProperties 配置類。對於 Spring 異步任務,會使用該執行器。
    execution:
      thread-name-prefix: async- # 線程池的線程名的前綴。默認為 task- ,建議根據自己應用來設置
      pool: # 線程池相關
        core-size: 8 # 核心線程數,線程池創建時候初始化的線程數。默認為 8 。
        max-size: 20 # 最大線程數,線程池最大的線程數,只有在緩沖隊列滿了之后,才會申請超過核心線程數的線程。默認為 Integer.MAX_VALUE
        keep-alive: 60s # 允許線程的空閑時間,當超過了核心線程之外的線程,在空閑時間到達之后會被銷毀。默認為 60 秒
        queue-capacity: 200 # 緩沖隊列大小,用來緩沖執行任務的隊列的大小。默認為 Integer.MAX_VALUE 。
        allow-core-thread-timeout: true # 是否允許核心線程超時,即開啟線程池的動態增長和縮小。默認為 true 。
      shutdown:
        await-termination: true # 應用關閉時,是否等待定時任務執行完成。默認為 false ,建議設置為 true
        await-termination-period: 60 # 等待任務完成的最大時長,單位為秒。默認為 0 ,根據自己應用來設置

同步與異步對比

@Component
public class DemoAsyncTask {

    @Scheduled(cron = "0/1 * * * * *")
    public void synTask() {
        sleep();
        System.out.println(Thread.currentThread().getName() + " syn-task 執行,當前時間: " + LocalDateTime.now());
    }

    @Async
    @Scheduled(cron = "0/1 * * * * *")
    public void asyncTask() {
        sleep();
        System.out.println(Thread.currentThread().getName() + " async-task 執行,當前時間: " + LocalDateTime.now());
    }

    private void sleep() {
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

同時開啟同步和異步任務,假設任務本身耗時較長,且間隔較短:間隔1s,執行10s,同步與異步執行的差異就此體現。

可以看到,同步任務並沒有每間隔1s就執行,而是串行在一起,等前一個任務執行完才執行。而異步任務則不一樣,成功將串行化的任務並行化。

源碼下載

本文內容均為對優秀博客及官方文檔總結而得,原文地址均已在文中參考閱讀處標注。最后,文中的代碼樣例已經全部上傳至Gitee:https://gitee.com/tqbx/springboot-samples-learn,另有其他SpringBoot的整合哦。

參考閱讀


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM