Issue
I am trying to fix/debug an issue of too many closing connection in a spring-boot web app that uses embedded tomcat. The problem arise because it closes connection that should be kept alive.
Now, I found that tomcat has configuration that limit the number of keep-alive connection (see maxKeepAliveRequests
in https://tomcat.apache.org/tomcat-8.5-doc/config/http.html) and there are maybe other config that could be related to the issue. But my problem is that I don't see where are those parameters given, or how I could change them if default are used.
My question: where can I found a documentation that explain how to config spring-boot/embedded-tomcat keep-alive parameters, and which are those parameters?
Solution
Not all tomcat properties can be configured via the properties file. The keep-alive
related properties are one of those properties and that means they can only be configured programmatically. This is done by configuring a WebServerFactoryCustomizer
bean. You can use the protocol handler to set the KeepAlive
settings. An example below:
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
return (tomcat) -> tomcat.addConnectorCustomizers((connector) -> {
if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
AbstractHttp11Protocol<?> protocolHandler = (AbstractHttp11Protocol<?>) connector
.getProtocolHandler();
protocolHandler.setKeepAliveTimeout(80000);
protocolHandler.setMaxKeepAliveRequests(500);
protocolHandler.setUseKeepAliveResponseHeader(true);
}
});
}
To know more about these settings please read the tomcat 9 configuration reference
Answered By - Maurice
Answer Checked By - Marie Seifert (JavaFixing Admin)