异步REST API生成警告 [英] Asynchronous REST API generating warning

查看:26
本文介绍了异步REST API生成警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Spring引导应用程序。我有一个返回Callable的REST控制器。

@GetMapping("/fb-roles")
@Timed
public Callable<List<FbRole>> getAllFbRoles() {
    log.debug("REST request to get all FbRoles");
    return (() -> { return fbRoleRepository.findAll(); });
}

ThreadPoolTaskExecutor配置如下:

@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {

private final Logger log = LoggerFactory.getLogger(AsyncConfiguration.class);

private final JHipsterProperties jHipsterProperties;

public AsyncConfiguration(JHipsterProperties jHipsterProperties) {
    this.jHipsterProperties = jHipsterProperties;
}

@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("fb-quiz-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}

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

}

警告10104-[XNIO-2 TASK-28]o.s.w.c.quest.async.WebAsyncManager: !!! 需要一个Executor来处理java.util.conCurent.Callable返回值。 请在MVC配置中的"Async Support"下配置TaskExecutor。 当前正在使用的SimpleAsyncTaskExecutor不适合负载。

但在访问API服务器时生成以下警告

推荐答案

Spring配置在这方面有点令人困惑,因为它需要对MVC异步支持进行单独的配置,即使用返回Callable的控制器处理程序方法,以及使用@Async注释的任何Spring Bean方法。要正确配置这两个配置,您可以应用类似下面的配置,但请记住,AsyncTaskExecutor配置可能需要修改:

@Configuration
@EnableAsync
public class AsyncConfig  implements AsyncConfigurer {

    @Bean
    protected WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
                configurer.setTaskExecutor(getTaskExecutor());
            }
        };
    }

    @Bean
    protected ConcurrentTaskExecutor getTaskExecutor() {
        return new ConcurrentTaskExecutor(Executors.newFixedThreadPool(5));
    }
}
顺便说一句,您可能想简单地用@Async注释您的控制器处理程序方法。这只会有预期的效果--释放Web服务器线程--启动并忘记操作(这是基于Spring Boot 2.1.2的观察,他们可能会在未来解决这个问题)。如果您想要利用Servlet 3.0异步处理的强大功能,您真的必须使用Callables并为它们配置WebMvcConfigurer

这篇关于异步REST API生成警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆