欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot使用定時(shí)器@Scheduled不管用的解決

 更新時(shí)間:2021年12月28日 17:07:44   作者:悠_ran  
這篇文章主要介紹了springboot使用定時(shí)器@Scheduled不管用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用定時(shí)器@Scheduled不管用

如果是一開始就不能用就是沒寫@EnableScheduling注解,如果是用著用著不管用了 是因?yàn)锧Scheduled是單線程,有定時(shí)器在工作或者沒有運(yùn)行完畢,所以造成了線程堵塞所以導(dǎo)致下一個(gè)定時(shí)器不能運(yùn)行增加一個(gè)方法類

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));
    }
}

就好了!

多個(gè)@Scheduled定時(shí)器不執(zhí)行

最近項(xiàng)目中經(jīng)常有用到@Scheduled注解,在內(nèi)測(cè)時(shí)由于數(shù)據(jù)量小(沒有進(jìn)行壓力測(cè))所以每個(gè)線程執(zhí)行都很快,但線上后發(fā)現(xiàn)部分功能無法使用,最后定位是部分的定時(shí)器沒有執(zhí)行,后查閱資料和Springboot源碼后

ScheduledTaskRegistrar在啟動(dòng)時(shí),如果沒有指定線程池的大小,默認(rèn)會(huì)創(chuàng)建核心線程數(shù)為1的默認(rèn)線程池,故而當(dāng)項(xiàng)目中出現(xiàn)多個(gè)@Scheduled線程時(shí),只能一個(gè)個(gè)的執(zhí)行,從而導(dǎo)致個(gè)別線程執(zhí)行時(shí)間過長(zhǎng)(或長(zhǎng)期執(zhí)行)時(shí),其他定時(shí)器不能按照指定的規(guī)則進(jìn)行執(zhí)行。

解決方法

1.在項(xiàng)目初始化時(shí)指定其執(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è)置
 * 描述 : 讀取項(xiàng)目中使用了@Scheduled注解的方法,默認(rèn)所有方法在項(xiàng)目創(chuàng)建時(shí)都需要按照設(shè)定的規(guī)則執(zhí)行
 * 備注 : //TODO
 * <p>
 * 修改歷史
 * 序號(hào) 	       日期 		        修改人 		         修改原因
 */
@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í)器設(shè)置為異步線程

/**
異步線程
定時(shí)器延遲1秒啟動(dòng),每距上一次執(zhí)行完成后間隔3秒執(zhí)行一次
*/
@Async("taskExecutor")
@Scheduled(initialDelay = 1000L, fixedDelay = 3000L)
public void test(){
   System.out.println("---"+System.currentTimeMillis());
 //業(yè)務(wù)內(nèi)容
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis多數(shù)據(jù)源的兩種配置方式

    MyBatis多數(shù)據(jù)源的兩種配置方式

    這篇文章主要給大家介紹了關(guān)于MyBatis多數(shù)據(jù)源的兩種配置方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Java中線程的基本方法使用技巧

    Java中線程的基本方法使用技巧

    這篇文章主要介紹了Java中線程的基本方法使用技巧,需要的朋友可以參考下
    2017-09-09
  • spring mvc常用注解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    spring mvc常用注解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了spring mvc常用注解,詳細(xì)的介紹了@RequestMapping, @RequestParam, @ModelAttribute等等這樣類似的注解,有興趣的可以了解一下
    2017-08-08
  • Spring JPA 錯(cuò)題集解決案例

    Spring JPA 錯(cuò)題集解決案例

    這篇文章主要為大家介紹了Spring JPA 錯(cuò)題集解決案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Spring中如何獲取request的方法匯總及其線程安全性分析

    Spring中如何獲取request的方法匯總及其線程安全性分析

    這篇文章主要給大家介紹了關(guān)于Spring中如何獲取request的方法匯總及其線程安全性分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • 最新評(píng)論