1. 程式人生 > 程式設計 >SpringBoot配置及使用Schedule過程解析

SpringBoot配置及使用Schedule過程解析

我們在平常專案開發中,經常會用到週期性定時任務,這個時候使用定時任務就能很方便的實現。在SpringBoot中用得最多的就是Schedule。

一、SpringBoot整合Schedule

1、依賴配置

由於Schedule就包含在spring-boot-starter中,所以無需引入其他依賴。

2、啟用定時任務

在啟動類或者配置類上增加@EnableScheduling註解。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class DemoApplication {

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

3、新增定時任務

Schdule支援cron表示式、固定間隔時間、固定頻率三種排程方式。

1)cron表示式定時任務

與Linux下定時任務用到的Cron表示式一樣。

欄位 允許值 允許的特殊字元
秒(Seconds) 0~59的整數 ,- * / 四個字元
分(Minutes) 0~59的整數 ,- * / 四個字元
小時(Hours) 0~23的整數 ,- * / 四個字元
日期(DayofMonth) 1~31的整數(但是你需要考慮該月的天數) ,- * ? / L W C 八個字元
月份(Month) 1~12的整數或者 JAN-DEC ,- * / 四個字元
星期(DayofWeek) 1~7的整數或者 SUN-SAT (1=SUN) ,- * ? / L C # 八個字元
年(可選,留空)(Year) 1970~2099 ,- * / 四個字元

@Component
@EnableScheduling
public class MyCronTask {

  private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class); 

  @Scheduled(cron = "0/1 * * * * *")
  void cronSchedule(){
    logger.info("cron schedule execute");
  }

}

PS:Cron表示式方式配置的定時任務如果其執行時間超過排程頻率時,排程器會在下個執行週期執行。如第一次執行從第0秒開始,執行時長3秒,則下次執行為第4秒。

2)固定間隔定時任務

下一次的任務執行時間是從上一次定時任務結束時間開始計算。

@Scheduled(fixedDelay = 2)
void fixedDelaySchedule() throws Exception{
  Thread.sleep(2000);
  logger.info("fixed delay schedule execute");
}

輸出:

2020-04-23 23:11:54.362 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute
2020-04-23 23:11:58.365 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute
2020-04-23 23:12:02.372 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute
2020-04-23 23:12:06.381 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute

3)固定頻率定時任務

按照指定頻率執行任務

@Scheduled(fixedRate = 2000)
void fixedRateSchedule() throws Exception{
  Thread.sleep(3000);
  logger.info("fixed rate schedule execute");
}

輸出:

2020-04-23 23:16:14.750 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:17.754 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:20.760 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:23.760 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:26.764 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute

PS:當方法的執行時間超過任務排程頻率時,排程器會在當前方法執行完成後立即執行下次任務。

二、配置多個定時任務併發執行

1、並行or序列?

預設狀態下,當我們沒有給定時任務配置執行緒池時,Schedule是序列執行,如下:

@Component
@EnableScheduling
public class MyCronTask {

  private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class);
  
  @Scheduled(fixedDelay = 2000)
  void task1Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task1 execute");
  }

  @Scheduled(fixedDelay = 2000)
  void task2Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task2 execute");
  }

  @Scheduled(fixedDelay = 2000)
  void task3Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task3 execute");
  }
}

輸出:

2020-04-23 23:19:46.970 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:19:48.973 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:19:50.974 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:19:52.978 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:19:54.984 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:19:56.984 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task3 execute

可以看出來只有一個執行緒穿行執行所有定時任務。

2、Schedule並行執行配置

定時排程的並行化,有兩種配置方式:

1)修改任務排程器預設使用的執行緒池:新增一個configuration,實現SchedulingConfigurer介面就可以了。

@Configuration
public class ScheduleConfig implements SchedulingConfigurer{

  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.setTaskScheduler(getTaskScheduler());
  }

  @Bean
  public TaskScheduler getTaskScheduler() {
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(3);
    taskScheduler.setThreadNamePrefix("myworker-");
    taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    return taskScheduler;
  }
}

再次執行後,輸出:

2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:33:18.203 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:33:18.203 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:33:18.204 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task1 execute

2)直接將任務交給一步執行緒池處理:啟用@EnableAsync註解,並在每一個定時任務方法上使用@Async註解。

@Component
@EnableScheduling
@EnableAsync
@Async
public class MyCronTask {

  private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class);

  @Scheduled(fixedDelay = 2000)
  void task1Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task1 execute");
  }

  @Scheduled(fixedDelay = 2000)
  void task2Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task2 execute");
  }

  @Scheduled(fixedDelay = 2000)
  void task3Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task3 execute");
  }
}

輸出如下:

2020-04-23 23:38:00.614 INFO 85468 --- [ task-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:38:00.614 INFO 85468 --- [ task-3] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:38:00.614 INFO 85468 --- [ task-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:38:02.620 INFO 85468 --- [ task-4] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:38:02.620 INFO 85468 --- [ task-5] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:38:02.620 INFO 85468 --- [ task-6] com.springboot.study.tasks.MyCronTask : task3 execute

有上面輸出可以看出來這種方式對於每一次定時任務的執行都會建立新的執行緒,這樣對記憶體資源是一種浪費,嚴重情況下還會導致服務掛掉,因此為了更好控制執行緒的使用,我們可以自定義執行緒池。

首先配置執行緒池:

@Configuration
public class MyTaskExecutor {

  @Bean(name = "myExecutor")
  public TaskExecutor getMyExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(3);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.setQueueCapacity(20);
    taskExecutor.setThreadNamePrefix("myExecutor-");
    taskExecutor.initialize();
    return taskExecutor;
  }
}

使用我們自己的執行緒池:

@Component
@EnableScheduling
@EnableAsync
@Async("myExecutor")
public class MyCronTask {

  private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class);

  @Scheduled(fixedDelay = 2000)
  void task1Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task1 execute");
  }

  @Scheduled(fixedDelay = 2000)
  void task2Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task2 execute");
  }

  @Scheduled(fixedDelay = 2000)
  void task3Schedule() throws Exception{
    Thread.sleep(2000);
    logger.info("task3 execute");
  }
}

輸出:

2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task3 execute

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。