java notinited错误_解决:java.lang.IllegalStateException: ThreadPoolTaskScheduler not initialized报错...

这个是Spring boot多线程实现定时任务时的报错,解决方案在最下面;

报错信息:

java.lang.IllegalStateException: ThreadPoolTaskScheduler not initialized

at org.springframework.util.Assert.state(Assert.java:70) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler.getScheduledExecutor(ThreadPoolTaskScheduler.java:155) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler.schedule(ThreadPoolTaskScheduler.java:297) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleCronTask(ScheduledTaskRegistrar.java:409) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleTasks(ScheduledTaskRegistrar.java:349) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:330) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:273) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:204) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:98) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]

at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]

at com.ai.rai.group.system.SystemBootStartApplication.main(SystemBootStartApplication.java:41) [classes/:na]

代码:

package com.ai.rai.group.system;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;

import org.springframework.context.annotation.Bean;

import org.springframework.scheduling.TaskScheduler;

import org.springframework.scheduling.annotation.AsyncConfigurer;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.scheduling.annotation.SchedulingConfigurer;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

/**

* @version 1.0

* @ClassName ScheduleConfig

* @Description todo

* @Author 74981

* @Date 2018/8/31 16:24

*/

@Component

@EnableScheduling

public class ScheduleConfig implements SchedulingConfigurer, AsyncConfigurer {

/*

* 并行任务

*/

public void configureTasks(ScheduledTaskRegistrar taskRegistrar)

{

TaskScheduler taskScheduler = taskScheduler();

taskRegistrar.setTaskScheduler(taskScheduler);

}

/**

* 并行任务使用策略:多线程处理

*

* @return ThreadPoolTaskScheduler 线程池

*/

@Bean(destroyMethod = "shutdown")

public ThreadPoolTaskScheduler taskScheduler()

{

ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();

scheduler.setPoolSize(20);

scheduler.setThreadNamePrefix("task-");

scheduler.setAwaitTerminationSeconds(60);

scheduler.setWaitForTasksToCompleteOnShutdown(true);

return scheduler;

}

/*

* 异步任务

*/

public Executor getAsyncExecutor()

{

Executor executor = taskScheduler();

return executor;

}

/*

* 异步任务 异常处理

*/

public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler()

{

return new SimpleAsyncUncaughtExceptionHandler();

}

}

原因是ThreadPoolTaskScheduler没有初始化,

在第45行加上:

scheduler.initialize();

就可以了;

参考:https://stackoverflow.com/questions/41672364/spring-async-threadpooltaskscheduler-not-initialized

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值