SpringBoot自定義線程池,執(zhí)行定時任務方式
SpringBoot自定義線程池,執(zhí)行定時任務
在我們開發(fā)過程中有很多場景需要定時執(zhí)行,接下來我們在SpringBoot中實現定時任務的功能。
一、 幾個必要的注解
1、@EnableScheduling:在啟動類上添加此注解,幫助我們開啟定時任務。
2、@Scheduled:在實現接口上添加此注解,表示此接口需要定時調用執(zhí)行,這個注解的參數有不少,在文章最后展示,可以自己了解下。
3、@Component:在需要執(zhí)行定時任務的實現類上添加此注解。
4、@Configuration:自定義多線程使用
5、@EnableAsync:在自定義多線程配置類上添加
6、@Async:在執(zhí)行定時任務的方法上添加,表示使用自定義線程
二、 單線程實現定時任務
單線程場景在實際開發(fā)中并不多見,我們只用來測試
直接上代碼
1、啟動類添加@EnableScheduling注解
@SpringBootApplication
@EnableScheduling
public class LogaopApplication {
public static void main(String[] args) {
SpringApplication.run(LogaopApplication.class, args);
}
}
2、在執(zhí)行定時任務的實現類添加@Component注解,在執(zhí)行定時任務的方法上添加@Scheduled注解
@Service
@Slf4j
@Component
public class ScheduTestServiceImpl implements ScheduTestService {
@Autowired
private ScheduJobDao scheduJobDao;
@Scheduled(cron = "*/1 * * * * ?") // 一秒執(zhí)行一次
@Override
public void taskTest() throws InterruptedException {
System.out.println("測試定時任務------");
}
執(zhí)行結果
2023-09-16 11:17:17.978 INFO 4636 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'asyncServiceExecutor'
2023-09-16 11:17:18.183 INFO 4636 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2023-09-16 11:17:18.214 INFO 4636 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-16 11:17:18.230 INFO 4636 --- [ main] com.qty.logaop.LogaopApplication : Started LogaopApplication in 1.98 seconds (JVM running for 2.555)
測試定時任務------
測試定時任務------
測試定時任務------
測試定時任務------
測試定時任務------
測試定時任務------
測試定時任務------
以上就是單線程執(zhí)行定時任務,接下來測試多線程定時任務。
三、自定義多線程執(zhí)行定時任務
因為springboot默認是提供單線程供我們使用,一旦有多個定時任務,當某個任務被阻塞,那么其他定時任務也不會被執(zhí)行,所以,一般我們會自定義線程池來解決這個問題
多個定時任務場景,某個阻塞情況演示
@Service
@Slf4j
@Component
public class ScheduTestServiceImpl implements ScheduTestService {
@Autowired
private ScheduJobDao scheduJobDao;
@Scheduled(cron = "*/1 * * * * ?")
@Override
public void taskTest() throws InterruptedException {
long timeStamp1 = System.currentTimeMillis();
Thread.sleep(1000*10);
System.out.println("測試定時任務1------");
long timeStamp2 = System.currentTimeMillis();
log.info("阻塞市時長:{}",timeStamp2-timeStamp1);
}
@Scheduled(cron = "*/1 * * * * ?")
@Override
public void taskTest2() throws InterruptedException {
System.out.println("測試定時任務2------");
}
演示結果
2023-09-16 11:28:07.906 INFO 19692 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'asyncServiceExecutor'
2023-09-16 11:28:08.157 INFO 19692 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2023-09-16 11:28:08.189 INFO 19692 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-16 11:28:08.204 INFO 19692 --- [ main] com.qty.logaop.LogaopApplication : Started LogaopApplication in 1.903 seconds (JVM running for 2.457)
測試定時任務1------
2023-09-16 11:28:19.035 INFO 19692 --- [ scheduling-1] c.q.l.s.impl.ScheduTestServiceImpl : 阻塞市時長:10015
測試定時任務2------
測試定時任務1------
2023-09-16 11:28:30.010 INFO 19692 --- [ scheduling-1] c.q.l.s.impl.ScheduTestServiceImpl : 阻塞市時長:10001
測試定時任務2------
這樣就會造成很大的問題,必須執(zhí)行完某個定時任務才會執(zhí)行下一個,所以我們需要自定義多線程解決這個問題
1、線程池配置類
package com.qty.logaop.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ScheduThreadPool {
@Value("${async.executor.thread.core_pool_size}")//從配置文件中獲取配置項
private int corePoolSize;
@Value("${async.executor.thread.max_pool_size}")
private int maxPoolSize;
@Value("${async.executor.thread.queue_capacity}")
private int queueCapacity;
@Value("${async.executor.thread.name.prefix}")
private String namePrefix;
/**
* 自定義線程池配置類。
* 不要命名為 taskScheduler,與spring框架的bean重名。
* @return
*/
@Bean(name = "asyncServiceExecutor")
public Executor asyncServiceExecutor() {
//阿里巴巴編程規(guī)范:線程池不允許使用 Executors 去創(chuàng)建,而是通過 ThreadPoolExecutor 的方式,這樣的處理方式讓寫的同學更加明確線程池的運行規(guī)則,規(guī)避資源耗盡的風險。
//SpringBoot項目,可使用Spring提供的對 ThreadPoolExecutor 封裝的線程池 ThreadPoolTaskExecutor:
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// ThreadPoolTaskExecutor executor = new MyThreadPoolTaskExecutor();//自定義ThreadPoolTaskExecutor,會打印線程池情況
//配置核心線程數
executor.setCorePoolSize(corePoolSize);
//配置最大線程數
executor.setMaxPoolSize(maxPoolSize);
//配置隊列大小
executor.setQueueCapacity(queueCapacity);
//配置線程池中的線程的名稱前綴
executor.setThreadNamePrefix(namePrefix);
// rejection-policy:當pool已經達到max size的時候,如何處理新任務
// 1、CallerRunsPolicy:不在新線程中執(zhí)行任務,而是由調用者所在的線程來執(zhí)行。
// "該策略既不會拋棄任務,也不會拋出異常,而是將任務回推到調用者。"顧名思義,在飽和的情況下,調用者會執(zhí)行該任務(而不是由多線程執(zhí)行)
// 2、AbortPolicy:拒絕策略,直接拒絕拋出異常
// 3、。。。
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//執(zhí)行初始化
executor.initialize();
return executor;
}
}
我們在方法上添加@Async注解后再看結果
@Scheduled(cron = "*/10 * * * * ?")
@Async("asyncServiceExecutor")
@Override
public void taskTest() throws InterruptedException {
long timeStamp1 = System.currentTimeMillis();
Thread.sleep(1000*100);
System.out.println("測試定時任務1------");
long timeStamp2 = System.currentTimeMillis();
log.info("阻塞市時長:{}",timeStamp2-timeStamp1);
}
@Scheduled(cron = "*/10 * * * * ?")
@Async("asyncServiceExecutor")
@Override
public void taskTest2() throws InterruptedException {
System.out.println("測試定時任務2------");
}
演示結果
2023-09-16 11:43:25.065 INFO 11604 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-16 11:43:25.065 INFO 11604 --- [ main] com.qty.logaop.LogaopApplication : Started LogaopApplication in 1.909 seconds (JVM running for 2.561)
測試定時任務2------
測試定時任務2------
測試定時任務2------
即使定時任務1被阻塞,定時任務2會獲取到其他線程,繼續(xù)執(zhí)行的,這樣就可以解決多個定時任務的問題了。
@Scheduled參數:
- 0 0 10,14,16 * * ? 每天上午10點,下午2點,4點每天上午10點,下午2點,4點
- 0 0/30 9-17 * * ? 朝九晚五工作時間內每半小時
- 0 0 12 ? * WED 表示每個星期三中午12點
- 0 0 12 * * ? 每天中午12點觸發(fā)
- 0 15 10 ? * * 每天上午10:15觸發(fā)
- 0 15 10 * * ? 每天上午10:15觸發(fā)
- 0 15 10 * * ? * 每天上午10:15觸發(fā)
- 0 15 10 * * ? 2019 2019年的每天上午10:15觸發(fā)
- 0 * 14 * * ? 在每天下午2點到下午2:59期間的每1分鐘觸發(fā)
- 0 0/5 14 * * ? 在每天下午2點到下午2:55期間的每5分鐘觸發(fā)
- 0 0/5 14,18 * * ? 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發(fā)
- 0 0-5 14 * * ? 在每天下午2點到下午2:05期間的每1分鐘觸發(fā)
- 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44觸發(fā)
- 0 15 10 ? * MON-FRI 周一至周五的上午10:15觸發(fā)
- 0 15 10 15 * ? 每月15日上午10:15觸發(fā)
- 0 15 10 L * ? 每月最后一日的上午10:15觸發(fā)
- 0 15 10 ? * 6L 每月的最后一個星期五上午10:15觸發(fā)
- 0 15 10 ? * 6L 2018-2019 2018年至2019年的每月的最后一個星期五上午10:15觸發(fā)
- 0 15 10 ? * 6#3 每月的第三個星期五上午10:15觸發(fā)
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring中@PathVariable和@RequestParam注解的用法區(qū)別
這篇文章主要介紹了Spring中@PathVariable和@RequestParam注解的用法區(qū)別,@PathVariable 是 Spring 框架中的一個注解,用于將 URL 中的變量綁定到方法的參數上,它通常用于處理 RESTful 風格的請求,從 URL 中提取參數值,并將其傳遞給方法進行處理,需要的朋友可以參考下2024-01-01
springboot使用jasypt對配置文件加密加密數據庫連接的操作代碼
這篇文章主要介紹了springboot使用jasypt對配置文件加密加密數據庫連接的操作代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01

