SpringBoot實現(xiàn)定時任務的三種方式小結
SpringBoot三種方式實現(xiàn)定時任務
定時任務實現(xiàn)的三種方式
Timer:這是java自帶的java.util.Timer類,這個類允許你調度一個java.util.TimerTask任務。使用這種方式可以讓你的程序按照某一個頻度執(zhí)行,但不能在指定時間運行。一般用的較少。ScheduledExecutorService:也jdk自帶的一個類;是基于線程池設計的定時任務類,每個調度任務都會分配到線程池中的一個線程去執(zhí)行,也就是說,任務是并發(fā)執(zhí)行,互不影響。Spring Task:Spring3.0以后自帶的task,可以將它看成一個輕量級的Quartz,而且使用起來比Quartz簡單許多。
使用Timer
這個目前在項目中用的較少,直接貼demo代碼。具體的介紹可以查看api
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();
//安排指定的任務在指定的時間開始進行重復的固定延遲執(zhí)行。這里是每3秒執(zhí)行一次
timer.schedule(timerTask,10,3000);
}
}
使用ScheduledExecutorService
該方法跟Timer類似,直接看demo:
public class TestScheduledExecutorService {
public static void main(String[] args) {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// 參數(shù):1、任務體 2、首次執(zhí)行的延時時間
// 3、任務執(zhí)行間隔 4、間隔時間單位
service.scheduleAtFixedRate(()->System.out.println("task ScheduledExecutorService "+new Date()), 0, 3, TimeUnit.SECONDS);
}
}
使用Spring Task
1.簡單的定時任務
在SpringBoot項目中,我們可以很優(yōu)雅的使用注解來實現(xiàn)定時任務,首先創(chuàng)建項目,導入依賴:
<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>
創(chuàng)建任務類:
@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注解開啟對定時任務的支持,然后啟動項目
可以看到三個定時任務都已經執(zhí)行,并且使同一個線程中串行執(zhí)行,如果只有一個定時任務,這樣做肯定沒問題,當定時任務增多,如果一個任務卡死,會導致其他任務也無法執(zhí)行。
2.多線程執(zhí)行
在傳統(tǒng)的Spring項目中,我們可以在xml配置文件添加task的配置,而在SpringBoot項目中一般使用config配置類的方式添加配置,所以新建一個AsyncConfig類
@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
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
JAVA線程池監(jiān)控以及動態(tài)調整示例詳解
這篇文章主要為大家介紹了JAVA線程池監(jiān)控以及動態(tài)調整示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
eclipse輸出Hello World的實現(xiàn)方法
這篇文章主要介紹了eclipse輸出Hello World的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

