4

I need to Timeout the request if no response is received in 5s. I tried server.tomcat.connection-timeout, but No Luck.

I am aware of spring.mvc.async.request-timeout property using Callable<?>, but server.tomcat.connection-timeout should work ,right?

application.properties

server.tomcat.connection-timeout=5s

Rest 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>
user15006167
  • 184
  • 1
  • 3
  • 12
  • How are you running your application (jar or deploying a war)? Which Spring Boot version are you using? Please provide additional information. – M. Deinum Dec 07 '21 at 08:04
  • 1
    Try changing Callable signature, if you use spring mvc, refer: https://www.baeldung.com/spring-rest-timeout#spring-mvc-request-timeout – Huy Nguyen Dec 07 '21 at 08:17
  • @M.Deinum I have added the pom.xml – user15006167 Dec 07 '21 at 08:35
  • @huy but even server.tomcat.connection-timeout should work right? – user15006167 Dec 07 '21 at 08:35
  • you should add the config for spring mvc also spring.mvc.async.request-timeout=5000 and try again... – Huy Nguyen Dec 07 '21 at 08:44
  • 1
    The connection-timeout isn't what you think it is. See https://stackoverflow.com/questions/30839941/meaning-of-connectiontimeout-in-tomcat/30840242 and https://stackoverflow.com/questions/39643647/what-exactly-does-connectiontimeout-means-in-tomcat#:~:text=In%20the%20docs%20(Tomcat%207,no%20(i.e.%20infinite)%20timeout. In your case a proper request is send thus the timeout doesn't apply. – M. Deinum Dec 07 '21 at 09:06
  • @M.Deinum Ok got it, Thanks... If get into any infinite loop, i just want to come out of it. Is there anyway to timeout other than making use of Callable interface? – user15006167 Dec 07 '21 at 09:25
  • You won't come out either way. While the connection will timeout it doesn't kill the processing on the server, only the connection is closed. The server will continue the process anyway but cannot send the response. – M. Deinum Dec 07 '21 at 09:26
  • @M.Deinum Oh...I am so confused ... So basically we cannot kill the process and return a response or Throw an error if I run into an infinite loop?... Sorry, I am new to all these, so trying to catch up... – user15006167 Dec 07 '21 at 09:33

1 Answers1

1

There is no setting in Spring MVC to control the timeout of request handled by Controller unless of-course you are using Async processing which basically means you need to return a Callable<> if you want spring.mvc.async.request-timeout to work. The property you are mentioning server.tomcat.connection-timeout is actually a tomcat property ( which is set up by Spring Boot) which basically refers to timeout if the client opens a connection but is not sending or it's very slow to send the request ( uri, headers etc. as per http protocol)

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.

You can check this for an issue reported in Spring Boot and the comments by Spring team.

Shailendra
  • 8,874
  • 2
  • 28
  • 37