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>
CodePudding user response:
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.