Home > Software engineering >  How to configure low-level socket properties for a web server (Tomcat) embedded in my spring-boot ap
How to configure low-level socket properties for a web server (Tomcat) embedded in my spring-boot ap

Time:06-03

How can I configure low-level options for spring-boot embedded web server (in my concrete case I am using Tomcat)?

As an example, I would like set SO_REUSEADDR to true.

CodePudding user response:

This answer is Tomcat-specific

Turns out this is possible through TomcatConnectorCustomizer beans, e.g.

@Bean
public TomcatConnectorCustomizer tomcatConnectorCustomizer() {
    return connector -> connector.setProperty("socket.soReuseAddress", "true");
}

How does it work?

connector.setProperty is implemented with IntrospectionUtils.setProperty which looks for bean property with a matching name, and failing that, uses a getProperty method in the connector's ProtocolHandler.

In my case, the ProtocolHandler was org.apache.coyote.http11.Http11NioProtocol which has a setProperty in its superclass org.apache.coyote.AbstractProtocol, that in the end invokes org.apache.tomcat.util.net.AbstractEndpoint's setProperty.

That method then differentiates properties with socket. prefix, and again reflectively matches it to org.apache.tomcat.util.net.SocketProperties accessors (in our case setSoReuseAddress).

In the end, when the sockets get created, the stored configuration is used when configuring a new socket.


It should be also noted (again, this is Tomcat specific) that Tomcat also uses JVM defaults (link).

  • Related