简体   繁体   English

如何处理 spring 启动中的异步请求超时?

[英]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.

相关问题 如何在Spring Boot中处理请求中的空值? - How to handle null value in the request in Spring Boot? 如何在Spring Boot异步功能中处理未捕获的异常? - How to handle uncaught exceptions in Spring boot async function? 如何在Spring启动时创建部分异步发布请求? - How to create partly async post request in Spring boot? Spring Boot Async 方法如何使用 ThreadPool 处理请求 - How Spring Boot Async method handles request using ThreadPool 我如何处理在请求中作为“@PathVariable”出现的“ñ”字符,Spring Boot - How do i Handle a "ñ" character that comes as a "@PathVariable" in a request, Spring Boot Spring Boot调用rest ws SocketTimeoutException连接超时 - Spring Boot calling rest ws SocketTimeoutException connect timed out Spring Boot 电子邮件发送抛出 SocketTimeoutException:读取超时 - Spring Boot email sending throws SocketTimeoutException: Read timed out Spring 启动 REST API 服务器连接有时超时 - Spring Boot REST API server Connection timed out sometime 如何在Spring Boot中处理SQLIntegrityConstraintViolationException? - How to handle the SQLIntegrityConstraintViolationException in Spring Boot? 如何处理 Spring 启动中的 UnsupportedMediaTypeException? - How to handle UnsupportedMediaTypeException in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM