SpringBoot三種方式實現定時任務
定時任務實現的三種方式
-
Timer
:這是java自帶的java.util.Timer類,這個類允許你調度一個java.util.TimerTask任務。使用這種方式可以讓你的程序按照某一個頻度執行,但不能在指定時間運行。一般用的較少。 -
ScheduledExecutorService
:也jdk自帶的一個類;是基于線程池設計的定時任務類,每個調度任務都會分配到線程池中的一個線程去執行,也就是說,任務是并發執行,互不影響。 -
Spring Task
:Spring3.0以后自帶的task,可以將它看成一個輕量級的Quartz,而且使用起來比Quartz簡單許多。
使用Timer
這個目前在項目中用的較少,直接貼demo代碼。具體的介紹可以查看api
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class TestTimer { public static void main(String[] args) { TimerTask timerTask = new TimerTask() { @Override public void run() { System.out.println( "task run:" + new Date()); } }; Timer timer = new Timer(); //安排指定的任務在指定的時間開始進行重復的固定延遲執行。這里是每3秒執行一次 timer.schedule(timerTask, 10 , 3000 ); } } |
使用ScheduledExecutorService
該方法跟Timer類似,直接看demo:
1
2
3
4
5
6
7
8
|
public class TestScheduledExecutorService { public static void main(String[] args) { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); // 參數:1、任務體 2、首次執行的延時時間 // 3、任務執行間隔 4、間隔時間單位 service.scheduleAtFixedRate(()->System.out.println( "task ScheduledExecutorService " + new Date()), 0 , 3 , TimeUnit.SECONDS); } } |
使用Spring Task
1.簡單的定時任務
在SpringBoot項目中,我們可以很優雅的使用注解來實現定時任務,首先創建項目,導入依賴:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > </ dependencies > |
創建任務類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Slf4j @Component public class ScheduledService { @Scheduled (cron = "0/5 * * * * *" ) public void scheduled(){ log.info( "=====>>>>>使用cron {}" ,System.currentTimeMillis()); } @Scheduled (fixedRate = 5000 ) public void scheduled1() { log.info( "=====>>>>>使用fixedRate{}" , System.currentTimeMillis()); } @Scheduled (fixedDelay = 5000 ) public void scheduled2() { log.info( "=====>>>>>fixedDelay{}" ,System.currentTimeMillis()); } } |
在主類上使用@EnableScheduling注解開啟對定時任務的支持,然后啟動項目
可以看到三個定時任務都已經執行,并且使同一個線程中串行執行,如果只有一個定時任務,這樣做肯定沒問題,當定時任務增多,如果一個任務卡死,會導致其他任務也無法執行。
2.多線程執行
在傳統的Spring項目中,我們可以在xml配置文件添加task的配置,而在SpringBoot項目中一般使用config配置類的方式添加配置,所以新建一個AsyncConfig類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Configuration @EnableAsync public class AsyncConfig { /* 此處成員變量應該使用@Value從配置中讀取 */ private int corePoolSize = 10 ; private int maxPoolSize = 200 ; private int queueCapacity = 10 ; @Bean public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.initialize(); return executor; } } |
-
@Configuration
:表明該類是一個配置類 -
@EnableAsync
:開啟異步事件的支持
然后在定時任務的類或者方法上添加@Async 。最后重啟項目,每一個任務都是在不同的線程中。
在線cron表達式生成:http://qqe2.com/cron/index
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/longzhongxiaoniao/article/details/86182075