异步REST API生成警告

Ari*_*ani 3 spring spring-mvc spring-boot jhipster spring-async

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

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

ThreadPoolTask​​Executor的配置如下:

@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();
}
Run Code Online (Sandbox Code Playgroud)

}

2018-09-19 00:43:58.434警告10104 --- [XNIO-2 task-28] oswcrequest.async.WebAsyncManager:!!! 需要执行程序来处理java.util.concurrent.Callable返回值。请在“异步支持”下的MVC配置中配置TaskExecutor。当前使用的SimpleAsyncTaskExecutor在负载下不适合。

但是在访问api服务器时会产生以下警告

Fri*_*rdt 5

在这方面,Spring配置有点令人困惑,因为它需要对MVC Async支持进行单独的配置,即使用返回a的Controller处理程序方法Callable以及使用注释的任何Spring bean方法@Async。要正确配置两者,您可以应用以下配置,请记住该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));
    }
}
Run Code Online (Sandbox Code Playgroud)

附带说明一下,您可能会很想用来简单地注释Controller处理程序方法@Async。这只会在火灾和遗忘操作上产生预期的效果-释放Web服务器线程-(此观察基于Spring Boot 2.1.2,将来可能会解决此问题)。如果您想利用Servlet 3.0异步处理的功能,您确实必须使用Callables和配置它们WebMvcConfigurer


Huy*_*yen 1

来自您的警告“请在 MVC 配置中的“异步支持”下配置一个 TaskExecutor 。当前使用的 SimpleAsyncTaskExecutor 不适合负载。”

不知道你是否使用了spring mvc?

对于 MVC,以下一些链接可能会有所帮助:

在springboot应用程序中配置mvc异步任务执行器

Spring Boot - 有设置 TaskExecutor 的快捷方式吗?