Spring Boot 2的侵入性AsyncRequestTimeoutException

时间:2018-08-26 18:51:14

标签: spring-mvc spring-boot server-sent-events

有人可以向我解释此异常的原因以及如何有效解决它吗?我不想实施here提供的解决方案,因为它可能会有一些副作用。当控制器的处理程序返回SseEmitter时,将发生此异常。

@GetMapping("/sse")
SseEmitter registerClient(){
  SseEmitter emitter = new SseEmitter()
  return emitter;
}

上面的代码导致:

org.springframework.web.context.request.async.AsyncRequestTimeoutException: null
    at org.springframework.web.context.request.async.TimeoutDeferredResultProcessingInterceptor.handleTimeout(TimeoutDeferredResultProcessingInterceptor.java:42)
    at org.springframework.web.context.request.async.DeferredResultInterceptorChain.triggerAfterTimeout(DeferredResultInterceptorChain.java:79)
    at org.springframework.web.context.request.async.WebAsyncManager.lambda$startDeferredResultProcessing$5(WebAsyncManager.java:426)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.springframework.web.context.request.async.StandardServletAsyncWebRequest.onTimeout(StandardServletAsyncWebRequest.java:151)
    at org.apache.catalina.core.AsyncListenerWrapper.fireOnTimeout(AsyncListenerWrapper.java:44)
    at org.apache.catalina.core.AsyncContextImpl.timeout(AsyncContextImpl.java:136)
    at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:153)
    at org.apache.coyote.AbstractProcessor.dispatch(AbstractProcessor.java:236)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
2018-08-25 | 20:49:00.116 | http-nio-8080-exec-3 |  WARN | o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver | Resolved exception caused by Handler execution: org.springframework.web.context.request.async.AsyncRequestTimeoutException

谢谢

2 个答案:

答案 0 :(得分:0)

Tomcat中的默认超时为30秒,但是您可以使用public SseEmitter(Long timeout)构造函数为此方法指定其值。超时值以毫秒为单位。

答案 1 :(得分:0)

如果您在 SseEmitter 上调用“完成”,您可以防止 org.springframework.web.context.request.async.AsyncRequestTimeoutException

SseEmitter sseEmitter = new SseEmitter(1L);
sseEmitter.onTimeout(sseEmitter::complete); // prevent AsyncRequestTimeoutException on timeout