3

I want my Spring Boot application with embedded Tomcat to reset connection if request is being processed more than 3 seconds. But there is no way I could do that. The last of my code snippets is:

@SpringBootApplication
@ComponentScan(basePackages = {"."})
@Controller
public class ExternalServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ExternalServerApplication.class, args);
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();

        factory.addConnectorCustomizers(
                connector -> {
                    AbstractProtocol protocol = (AbstractProtocol) connector.getProtocolHandler();
                    protocol.setConnectionTimeout(1);
                    protocol.setKeepAliveTimeout(1);
                    protocol.setSoTimeout(1);
                    protocol.setPort(8012);
                    protocol.setMaxThreads(10);
                });

        return factory;
    }

    @RequestMapping("/request")
    @ResponseBody
    public String request() throws InterruptedException {
        for (int i = 0; i < 50; i++) {
            Thread.sleep(100);
        }
        return "OK";
    }
}

But it doesn't work either.

The request to localhost:8012/requests lasts for 5 seconds and returns "OK", but it should be reseted.

Any ideas?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
corvax
  • 1,095
  • 1
  • 10
  • 35
  • Have you tried specifying: server.connection-timeout=3000 in your application.properties? Edit: On the other hand, You write about processing request timeout, while in my opinion server.connection-timeout is something else (quote from documentation: "The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented.") – patrykos91 Aug 19 '16 at 10:03
  • Take a look at that thread: http://stackoverflow.com/questions/7145131/tomcat-request-timeout Another option that can solve Your problem is to use Netflix Zuul :) – patrykos91 Aug 19 '16 at 10:11

1 Answers1

2

What you are looking for is a client timeout. The parameters that you have set - setConnectionTimeout, setKeepAliveTimeout, setSoTimeout - have different meanings.

As per apache tomcat documentation, below is a definition of these timeouts:

ConnectionTimeout - The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented

KeepAliveTimeout - The number of milliseconds this Connector will wait for another HTTP request before closing the connection

soTimeout - This is equivalent to standard attribute connectionTimeout

None of these match your requirement, which is:

  1. to consider the end-to-end time taken to process a request
  2. fail the request, if that time is beyond a preset value (3 sec, in your case)

Such a feature needs to be a part of the http client implementation and is specific to the http client, you are using.

Say for example, if you are using curl to make a http request, you can use curl's --max-time option to specify this, as shown below.

curl --max-time 3 http://localhost:8012/requests