java - SpringBoot : server. tomcat.connection-timeout 不工作

标签 java spring spring-boot

如果在 5 秒内没有收到响应,我需要暂停请求。我尝试了 server.tomcat.connection-timeout,但没有成功。

我知道 spring.mvc.async.request-timeout 属性使用 Callable,但是 server.tomcat.connection-timeout 应该可以,对吧?

应用程序属性

server.tomcat.connection-timeout=5s

休息 Controller

@PostMapping("/getdata")
public String call() throws Exception {

    Thread.sleep(10000);

    return "Hello";

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.revise</groupId>
    <artifactId>database</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>database</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

最佳答案

Spring MVC 中没有设置来控制 Controller 处理的请求的超时,当然除非您使用异步处理,这基本上意味着您需要返回一个 Callable<>。如果你想要 spring.mvc.async.request-timeout上类。你说的特性server.tomcat.connection-timeout实际上是一个 tomcat 属性(由 Spring Boot 设置),如果客户端打开连接但不发送或者发送请求(根据 http 协议(protocol)的 uri、 header 等)很慢,它基本上是指超时

The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of -1 to indicate no (i.e. infinite) timeout.

可以查看this对于 Spring Boot 中报告的问题和 Spring 团队的评论。

关于java - SpringBoot : server. tomcat.connection-timeout 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70256574/

相关文章:

java - Spring Boot 覆盖其他应用程序文件中的属性

java - 关闭 JDialog 时没有 WindowEvent

java - 不应在 android manifest 文件中声明 minSdk 版本

java - Spring JPA 映射 - 第一步

java - 我想读取带有 map 的 spring 属性文件

java - 用户能够访问管理员角色页面 - Spring Security

java - 如何在 spring boot 中从属性文件为 @Order 注释设置值

java - 将 JDBCTemplate 与 Hibernate SessionFactory 一起使用?

Java 字符串或对象的哈希集比较

java - 将对象添加到java mongodb中的数组