带有StreamingResponseBody async.request-timeout的@RestController不起作用 [英] @RestController with StreamingResponseBody async.request-timeout not working
问题描述
我正在尝试通过长时间运行的工作从@RestController获取StreamingResponseBody.无论我尝试哪种配置,它都会在30秒后超时.
I'm trying to get a StreamingResponseBody from a @RestController on a long running job. It times out after 30 seconds, no matter what configuration I try.
这是在Spring Boot 2.0.3中.我使用了下面的测试,该测试显示了相同的行为,试图正确配置.
This is in Spring Boot 2.0.3. I've used the below test which shows the same behavior to try and get the configuration correct.
@RestController
public class TestController {
@RequestMapping("/streamtest")
public StreamingResponseBody handleRequest () {
return new StreamingResponseBody() {
@Override
public void writeTo (OutputStream out) throws IOException {
for (int i = 0; i < 100000; i++) {
out.write((Integer.toString(i) + " - ").getBytes());
out.flush();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
}
我尝试了这里定义的async.request-timeout设置;异步超时,使用StreamingResponseBody下载大文件在Spring Boot上
I've tried the async.request-timeout setting defined here; Async timeout downloading a large file using StreamingResponseBody on Spring Boot
我尝试覆盖WebMvcConfig来设置超时.永远不会调用此方法.
I've tried overriding WebMvcConfig to set the timeout. This method is never called.
@EnableWebMvc
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(3600000);
WebMvcConfigurer.super.configureAsyncSupport(configurer);
}
}
我尝试定义ThreadPoolTaskExecutor:
I've tried defining the ThreadPoolTaskExecutor:
@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
return new ThreadPoolTaskExecutor();
}
}
它总是在30秒后超时并显示日志;
It always times out after 30 seconds with a log;
12:14:28.028 [http-nio-8080-exec-2] DEBUG c.b.b.bof_static.config.BofStaticExceptionHandler - Async timeout for GET [/streamtest]
12:14:28.028 [http-nio-8080-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
12:14:28.028 [http-nio-8080-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
12:14:28.030 [http-nio-8080-exec-2] DEBUG o.s.security.web.access.ExceptionTranslationFilter - Chain processed normally
我找不到其他解决方案.谁能指出缺少的东西吗?
I'm unable to find any other solutions. Can anyone point out what's missing?
推荐答案
在application.properties中设置以下选项可以解决此问题:
Setting the following option in application.properties can solve this:
spring.mvc.async.request-timeout = -1
但是,如果在代码中的任何地方实现 WebMvcConfigurer
,则上述选项将被忽略,因此您必须将其设置为以下内容:
However, if you implement WebMvcConfigurer
anywhere in your code, then the above option will be ignored, so you have to set it up as the following:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
// other config...
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(-1);
}
}
您也可以将其设置为正整数(毫秒).-1值将完全消除超时.
You can also set it to a positive integer (milliseconds). The value of -1 will completely remove the timeout.
这篇关于带有StreamingResponseBody async.request-timeout的@RestController不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!