Spring Asboot在配置AsyncTaskExecutor时消耗所有cpu

时间:2016-09-08 09:16:58

标签: java spring spring-boot visualvm reactor

我的Spring Boot应用程序中有以下配置:

@Configuration
@EnableAsync
@Slf4j
public class AsyncConfig {
    private static final int BUFFER = 1024;

    @Bean
    public AsyncTaskExecutor singleThreadAsyncTaskExecutor(Environment env) {
        RingBufferAsyncTaskExecutor rbAsyncExecutor = new RingBufferAsyncTaskExecutor(env);
        rbAsyncExecutor.setName("rb-executor");
        rbAsyncExecutor.setBacklog(BUFFER);
        rbAsyncExecutor.setProducerType(ProducerType.SINGLE);
        rbAsyncExecutor.setWaitStrategy(new YieldingWaitStrategy());

        log.info("Async task executor loaded");
        return rbAsyncExecutor;
    }
}

当我运行它时,cpu使用率达到100%(有时是100分):

enter image description here

使用visualvm进行调查,我看到了这个

enter image description here

但是,当我删除AsyncTaskExecutor的实例化时,CPU使用率达到0.4%,而visualvm只显示了1%的CPU使用率。
我在使用docker部署它时发现了这个问题,我看到我的主机使用率达到了上限 我尝试将缓冲区大小(它是2048)降低到1024但没有任何改变 没有这个bean,我的@Async服务不会异步工作。 (No TaskExecutor bean found for async processing

1 个答案:

答案 0 :(得分:2)

我想我已经解决了 我所做的是使用ThreadPoolTaskExecutor代替RingBuffer,如下所示

@Bean
public AsyncTaskExecutor getAsync(){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(7);
    executor.setMaxPoolSize(42);
    executor.setQueueCapacity(11);
    executor.setThreadNamePrefix("AsyncExec-");
    executor.initialize();
    return executor;
}

出于某种原因,ThreadPoolTaskExecutor比其他人轻 我是从spring framework doc

得到的
相关问题