您当前的位置: 首页 >  spring

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

创建spring异步任务,报错No qualifying bean of type ‘org.springframework.core.task.TaskExecutor‘ available

小志的博客 发布时间:2020-07-08 14:40:03 ,浏览量:0

1、报错时的配置类和报错结果如下图: 在这里插入图片描述 在这里插入图片描述2、解决方式:在配置类中实现AsyncConfigurer接口,注册taskExecutor执行任务,即可,图和代码如下: 在这里插入图片描述

package com.rf.springboot01.concurrentAndThread.t3;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
 * @description:
 * @author: xiaozhi
 * @create: 2020-07-08 13:57
 */
@Configuration
@ComponentScan("com.rf.springboot01.concurrentAndThread.t3")
@EnableAsync
public class Demo7Config implements AsyncConfigurer {
    //注册执行器
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(80);
        taskExecutor.setQueueCapacity(100);
        taskExecutor.initialize();//如果不初始化,导致找到不到执行器
        return taskExecutor;
    }
    // 用于捕获异步异常
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}

3、在重新启动,控制台输出无报错信息,如下图: 在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0384s