@RestController 与 StreamingResponseBody async.request-timeout 不起作用

Jay*_*Jay 3 rest asynchronous spring-boot

我正在尝试从长时间运行的作业中的 @RestController 获取 StreamingResponseBody 。无论我尝试什么配置,它都会在 30 秒后超时。

这是 Spring Boot 2.0.3 中的内容。我使用了下面的测试,它显示了相同的行为来尝试正确配置。

@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();
                    }
                }
            }
       };
   }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过此处定义的 async.request-timeout 设置; 在 Spring Boot 上使用 StreamingResponseBody 异步超时下载大文件

我尝试重写 WebMvcConfig 来设置超时。这个方法永远不会被调用。

@EnableWebMvc
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(3600000);
        WebMvcConfigurer.super.configureAsyncSupport(configurer);
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试定义 ThreadPoolTask​​Executor:

@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        return new ThreadPoolTaskExecutor();
    }
}
Run Code Online (Sandbox Code Playgroud)

有日志总是30秒后超时;

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
Run Code Online (Sandbox Code Playgroud)

我无法找到任何其他解决方案。谁能指出缺少什么吗?

Zan*_*tsu 5

在 application.properties 中设置以下选项可以解决此问题:

spring.mvc.async.request-timeout=-1

但是,如果您WebMvcConfigurer在代码中的任何位置实现,则上述选项将被忽略,因此您必须将其设置如下:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    // other config...
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(-1);
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以将其设置为正整数(毫秒)。值 -1 将完全消除超时。