首页 > 作文

springboot使用定时器@Scheduled不管用的解决

更新时间:2023-04-04 08:17:30 阅读: 评论:0

目录
使用定时器@scheduled不管用多个@scheduled定时器不执行解决方法

使用定时器@scheduled不管用

如果是一开始就不能用就是没写@enablescheduling注笑咪咪解,如果是用着用着不管用了 是因为@scheduled是单线程,有定时器在工作或者没有运行完毕,所以造成了线程堵塞所以导致下一个定时器不能运行增加一个方法类

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;@configurationpublic 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 = defaultpool第一次尝到雪糕size;        }        taskregistrar.tscheduler(executors.newscheduledthreadpool(corepoolsize));    }}

就好了!

多个@scheduled定时器不执行

最近项目中经常有用到@scheduled注解,在内测时由于数据量小(没有进行压力测)所以每个线程执行都很快,但线上后发现部分功能无法使用,最后定位是部分的定时器没有执行,后查阅资料和springboot源码后

scheduledtaskregistrar在启动时,如果没有指定线程池的大小,默认会创建核心线程数为1的默认线程池,故而当项目中出现多个@scheduled线程时,只能一个个的执行,从而导致个别线程执行时间过长(或长期执行)时,其他定时器不lol情人节活动能按照指定的规则进行执行。

解决方法

1.在项目初始化时指定其执行线程池的大小

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任务线程池设置 * 描述 : 读取项目中使用了@scheduled注解的方法,默认所有中华大蟾蜍方法在项目创建时都需要按照设定的规则执行 * 备注 : //todo * <p> * 修改历史 * 序号        日期         修改人          修改原因 */@configurationpublic class scheduledtaskconfiguration implements schedulingconfigurer {    @override    publi幼儿舞蹈班c 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.tscheduler(executor);    }}

2.将定时器设置为异步线程

/**异步线程定时器延迟1秒启动,每距上一次执行完成后间隔3秒执行一次*/@async("taskexecutor")@scheduled(initialdelay = 1000l, fixeddelay = 3000l)public void test(){   system.out.println("---"+system.currenttimemillis()); //业务内容}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 08:17:28,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/9415c27180d66173148d2abab041c700.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:springboot使用定时器@Scheduled不管用的解决.doc

本文 PDF 下载地址:springboot使用定时器@Scheduled不管用的解决.pdf

标签:定时器   线程   多个   项目
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图