[英]How to handle async request timed out in spring boot?
I am writing configuration for spring-boot application.我正在为 spring-boot 应用程序编写配置。 I am using
WebMvcConfigurer
interface.我正在使用
WebMvcConfigurer
接口。 I have set default timeout as 30 seconds as I have used SseEmitter()
for event handling (as SseEmitter
has by default timeout of 30 seconds).我已将默认超时设置为 30 秒,因为我使用
SseEmitter()
进行事件处理(因为SseEmitter
默认超时为 30 秒)。 However, after 30 seconds, it gives warning Async request timed out
.但是,30 秒后,它会发出警告
Async request timed out
。 Is there a way to set timeout again or to handle this error?有没有办法再次设置超时或处理此错误? Please help me how to resolve this issue.
请帮助我如何解决这个问题。 Thanks in advance:)
提前致谢:)
This is what I have written.这就是我写的。
@Configuration
public class EventConfiguration implements WebMvcConfigurer {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(30000);
}
}
I am getting following errors:我收到以下错误:
2019-10-30 11:35:02.711 WARN 10728 --- [nio-8090-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Async request timed out
2019-10-30 11:35:02.712 WARN 10728 --- [nio-8090-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.context.request.async.AsyncRequestTimeoutException]
2019-10-30 11:35:03.699 WARN 10728 --- [nio-8090-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Async request timed out
2019-10-30 11:35:03.701 WARN 10728 --- [nio-8090-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.context.request.async.AsyncRequestTimeoutException]
If you want to simply handle the exception then you can write an exception handler for the same, something like below如果你想简单地处理异常,那么你可以为它编写一个异常处理程序,如下所示
@ExceptionHandler(AsyncRequestTimeoutException.class)
public final ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, WebRequest request) {
....
....
}
If you want to do more, you can write your own TimeoutCallableProcessingInterceptor如果你想做更多,你可以编写自己的 TimeoutCallableProcessingInterceptor
@Bean
public CallableProcessingInterceptor callableProcessingInterceptor() {
return new TimeoutCallableProcessingInterceptor() {
@Override
public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception {
log.error("timeout!");
return super.handleTimeout(request, task);
}
};
}
Note I have not tried this, let us know if it works注意我还没有尝试过,让我们知道它是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.