No task executor bean found for async processing

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21019419/article/details/89516062

No task executor bean found for async processing: no bean of type TaskExecutor and no bean named 'taskExecutor' either

检查日志的时候发现一个打印,这么一看貌似想个错误一样,但是级别是info?
然后去找到上下文一看,原来是springboot提供的异步调用@Async产生的。

具体的说明可以参考:
https://juejin.im/post/5ab7ab936fb9a028df229651

然后去看看:
https://segmentfault.com/a/1190000011339882
我们关注这篇文章的最下面,小结部分。

部分参考:
https://kim-miao.iteye.com/blog/1310015

原来我的代码中没有指定executor,所以springboot找来找去找到了默认的,然后打了一串东西。

添加代码:

@SpringBootApplication
@ServletComponentScan
@EnableCaching
@EnableAsync
public class ApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }

	// 这里是新添加的线程池代码
    @Bean(name = "threadPoolTaskExecutor")
    public Executor threadPoolTaskExecutor() {
        // 使用 google的Guava包
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("my-pool-%d").build();
        // 构造一个线程池
        return new ThreadPoolExecutor(
                5,
                200,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024),
                namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());
    }
}

需要的pom:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

然后在异步的地方:

    /**
     * 如果没有特别指定,默认使用SimpleAsyncTaskExecutor,执行异步方法.
     */
    @Async("threadPoolTaskExecutor")
    public void xcx(String uid, String orderId) {
    .......

另外关于配置AsyncConfigurer请大家自行修改了,我这里没有使用了。

over。

猜你喜欢

转载自blog.csdn.net/qq_21019419/article/details/89516062