Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle async request timed out in spring boot?

I am writing configuration for spring-boot application. I am using WebMvcConfigurer interface. 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). However, after 30 seconds, it gives warning 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]
like image 738
Bhushan Mahajan Avatar asked Sep 21 '25 00:09

Bhushan Mahajan


1 Answers

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

@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

like image 56
Shailesh Chandra Avatar answered Sep 22 '25 16:09

Shailesh Chandra