springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問(wèn)題
@schedule 注解 是springboot 常用的定時(shí)任務(wù)注解,使用起來(lái)簡(jiǎn)單方便,但是如果定時(shí)任務(wù)非常多,或者有的任務(wù)很耗時(shí),會(huì)影響到其他定時(shí)任務(wù)的執(zhí)行,因?yàn)閟chedule 默認(rèn)是單線(xiàn)程的,一個(gè)任務(wù)在執(zhí)行時(shí),其他任務(wù)是不能執(zhí)行的.解決辦法是重新配置schedule,改為多線(xiàn)程執(zhí)行.只需要增加下面的配置類(lèi)就可以了.
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.lang.reflect.Method;
import java.util.concurrent.Executors;
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
Method[] methods = BatchProperties.Job.class.getMethods();
int defaultPoolSize = 3;
int corePoolSize = 0;
if (methods != null && methods.length > 0) {
for (Method method : methods) {
Scheduled annotation = method.getAnnotation(Scheduled.class);
if (annotation != null) {
corePoolSize++;
}
}
if (defaultPoolSize > corePoolSize)
corePoolSize = defaultPoolSize;
}
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(corePoolSize));
}
}
源碼 https://github.com/Yanyf765/demo_schedule
總結(jié)
以上所述是小編給大家介紹的springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- SpringBoot整合canal實(shí)現(xiàn)數(shù)據(jù)同步的示例代碼
- 關(guān)于SpringBoot整合Canal數(shù)據(jù)同步的問(wèn)題
- SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法
- 詳解SpringBoot 創(chuàng)建定時(shí)任務(wù)(配合數(shù)據(jù)庫(kù)動(dòng)態(tài)執(zhí)行)
- SpringBoot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)
- Springboot整個(gè)Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的示例代碼
- SpringBoot 定時(shí)任務(wù)遇到的坑
- springboot集成schedule實(shí)現(xiàn)定時(shí)任務(wù)
- springboot整合Quartz實(shí)現(xiàn)動(dòng)態(tài)配置定時(shí)任務(wù)的方法
- SpringBoot定時(shí)任務(wù)實(shí)現(xiàn)數(shù)據(jù)同步的方法
相關(guān)文章
MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析,有時(shí)候并不需要真正的刪除數(shù)據(jù),而是想邏輯刪除,方便數(shù)據(jù)恢復(fù),MyBatis-Plus可以很方便的實(shí)現(xiàn)邏輯刪除的功能,需要的朋友可以參考下2023-11-11
JDK1.6“新“特性Instrumentation之JavaAgent(推薦)
這篇文章主要介紹了JDK1.6“新“特性Instrumentation之JavaAgent,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Feign?日期格式轉(zhuǎn)換錯(cuò)誤的問(wèn)題
這篇文章主要介紹了Feign?日期格式轉(zhuǎn)換錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Lombok中@Builder和@SuperBuilder注解的用法案例
@Builder?是?lombok?中的注解,可以使用builder()構(gòu)造的Person.PersonBuilder對(duì)象進(jìn)行鏈?zhǔn)秸{(diào)用,給所有屬性依次賦值,這篇文章主要介紹了Lombok中@Builder和@SuperBuilder注解的用法,需要的朋友可以參考下2023-01-01
Java 使用Socket正確讀取數(shù)據(jù)姿勢(shì)
這篇文章主要介紹了Java 使用Socket正確讀取數(shù)據(jù)姿勢(shì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

