您好,欢迎访问一九零五行业门户网

springboot中schedule怎么解决定时任务不执行的问题

@schedule 注解 是springboot 常用的定时任务注解,使用起来简单方便,但是如果定时任务非常多,或者有的任务很耗时,会影响到其他定时任务的执行,因为schedule 默认是单线程的,一个任务在执行时,其他任务是不能执行的.解决办法是重新配置schedule,改为多线程执行.只需要增加下面的配置类就可以了.
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 = defaultpoolsize;    }    taskregistrar.setscheduler(executors.newscheduledthreadpool(corepoolsize));  }}
以上就是springboot中schedule怎么解决定时任务不执行的问题的详细内容。
其它类似信息

推荐信息