java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized

1.Configuration配置如下:

@Configuration
@EnableAsync
@Slf4j
public class TaskExecutorConfiguration implements AsyncConfigurer {

    @Value("${corePoolSize}")
    private String corePoolSize;
    @Value("${maxPoolSize}")
    private String maxPoolSize;
    @Value("${queueCapacity}")
    private String queueCapacity;


    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize((NumberUtils.isDigits(corePoolSize)) ?Integer.parseInt(corePoolSize):5);
        taskExecutor.setMaxPoolSize((NumberUtils.isDigits(maxPoolSize)) ?Integer.parseInt(maxPoolSize):15);
        taskExecutor.setQueueCapacity((NumberUtils.isDigits(queueCapacity)) ?Integer.parseInt(queueCapacity):35);
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }
}

2.異步方法

 

@Service
public class AsynTaskService {

    @Async
    public void executeAsynSendImageCommand(){
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(234);
    }
}

3.執行該異步方法時報如下錯誤:

java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized
4.解決方法,在configuration getAsynExecutor()方法里加上 taskExecutor.initialize();

   @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.initialize();
        taskExecutor.setCorePoolSize((NumberUtils.isDigits(corePoolSize)) ?        Integer.parseInt(corePoolSize):5);
        taskExecutor.setMaxPoolSize((NumberUtils.isDigits(maxPoolSize)) ?Integer.parseInt                     (maxPoolSize):15);
        taskExecutor.setQueueCapacity((NumberUtils.isDigits(queueCapacity)) ?                                                                                                    Integer.parseInt(queueCapacity):35);
        return taskExecutor;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章