springboot使用定時器@Scheduled不管用的解決
使用定時器@Scheduled不管用
如果是一開始就不能用就是沒寫@EnableScheduling注解,如果是用著用著不管用了 是因為@Scheduled是單線程,有定時器在工作或者沒有運(yùn)行完畢,所以造成了線程堵塞所以導(dǎo)致下一個定時器不能運(yùn)行增加一個方法類
package com.llt;
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));
}
}
就好了!
多個@Scheduled定時器不執(zhí)行
最近項目中經(jīng)常有用到@Scheduled注解,在內(nèi)測時由于數(shù)據(jù)量小(沒有進(jìn)行壓力測)所以每個線程執(zhí)行都很快,但線上后發(fā)現(xiàn)部分功能無法使用,最后定位是部分的定時器沒有執(zhí)行,后查閱資料和Springboot源碼后


ScheduledTaskRegistrar在啟動時,如果沒有指定線程池的大小,默認(rèn)會創(chuàng)建核心線程數(shù)為1的默認(rèn)線程池,故而當(dāng)項目中出現(xiàn)多個@Scheduled線程時,只能一個個的執(zhí)行,從而導(dǎo)致個別線程執(zhí)行時間過長(或長期執(zhí)行)時,其他定時器不能按照指定的規(guī)則進(jìn)行執(zhí)行。
解決方法
1.在項目初始化時指定其執(zhí)行線程池的大小
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.Arrays;
import java.util.Objects;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 程序名 : ScheduledTaskConfiguration
* 建立日期: 2021-02-23 9:33
* 模塊 : Scheduled任務(wù)線程池設(shè)置
* 描述 : 讀取項目中使用了@Scheduled注解的方法,默認(rèn)所有方法在項目創(chuàng)建時都需要按照設(shè)定的規(guī)則執(zhí)行
* 備注 : //TODO
* <p>
* 修改歷史
* 序號 日期 修改人 修改原因
*/
@Configuration
public class ScheduledTaskConfiguration implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
Method[] methods = BatchProperties.Job.class.getMethods();
final AtomicInteger corePoolSize = new AtomicInteger();
if (Objects.nonNull(methods) && methods.length > 0) {
Arrays.stream(methods).forEach(method -> {
final Scheduled annotation = method.getAnnotation(Scheduled.class);
if (Objects.nonNull(annotation)) {
corePoolSize.incrementAndGet();
}
});
}
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(corePoolSize.get());
taskRegistrar.setScheduler(executor);
}
}
2.將定時器設(shè)置為異步線程
/**
異步線程
定時器延遲1秒啟動,每距上一次執(zhí)行完成后間隔3秒執(zhí)行一次
*/
@Async("taskExecutor")
@Scheduled(initialDelay = 1000L, fixedDelay = 3000L)
public void test(){
System.out.println("---"+System.currentTimeMillis());
//業(yè)務(wù)內(nèi)容
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringData JPA審計功能(@CreatedDate與@LastModifiedDate)實現(xiàn)
Spring Data JPA的審計功能提供了一種強(qiáng)大而靈活的機(jī)制,用于自動跟蹤實體的創(chuàng)建和修改信息,通過使用@CreatedDate和@LastModifiedDate注解,開發(fā)者可以輕松地實現(xiàn)時間審計,感興趣的可以了解一下2025-04-04
Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析
這篇文章主要介紹了Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
IDEA 使用mybatis插件Free Mybatis plugin的步驟(推薦)
這篇文章主要介紹了IDEA 使用mybatis插件Free Mybatis plugin的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
java中將科學(xué)計數(shù)法轉(zhuǎn)換普通計數(shù)法的簡單方法
下面小編就為大家?guī)硪黄猨ava中將科學(xué)計數(shù)法轉(zhuǎn)換普通計數(shù)法的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
java執(zhí)行shell并獲取shell輸出日志方式
這篇文章主要介紹了java執(zhí)行shell并獲取shell輸出日志方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

