Commit 1def64e4 authored by Venil Noronha's avatar Venil Noronha Committed by Phillip Webb

Add Jetty `acceptors` and `selectors` support

Add support for Jetty `acceptors` and `selectors` configuration, either
directly on the `JettyEmbeddedServletContainerFactory` or via
`server.jetty.acceptors`/`server.jetty.selectors` server properties.

Fixes gh-5380
Closes gh-5649
parent 1bf10f29
...@@ -402,7 +402,7 @@ public class ServerProperties ...@@ -402,7 +402,7 @@ public class ServerProperties
return this.tomcat; return this.tomcat;
} }
private Jetty getJetty() { public Jetty getJetty() {
return this.jetty; return this.jetty;
} }
...@@ -950,11 +950,43 @@ public class ServerProperties ...@@ -950,11 +950,43 @@ public class ServerProperties
} }
private static class Jetty { public static class Jetty {
/**
* Number of acceptor threads to use.
*/
private Integer acceptors;
/**
* Number of selector threads to use.
*/
private Integer selectors;
public Integer getAcceptors() {
return this.acceptors;
}
public void setAcceptors(Integer acceptors) {
this.acceptors = acceptors;
}
public Integer getSelectors() {
return this.selectors;
}
public void setSelectors(Integer selectors) {
this.selectors = selectors;
}
void customizeJetty(ServerProperties serverProperties, void customizeJetty(ServerProperties serverProperties,
JettyEmbeddedServletContainerFactory factory) { JettyEmbeddedServletContainerFactory factory) {
factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders()); factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders());
if (this.acceptors != null) {
factory.setAcceptors(this.acceptors);
}
if (this.selectors != null) {
factory.setSelectors(this.selectors);
}
if (serverProperties.getMaxHttpHeaderSize() > 0) { if (serverProperties.getMaxHttpHeaderSize() > 0) {
customizeMaxHttpHeaderSize(factory, customizeMaxHttpHeaderSize(factory,
serverProperties.getMaxHttpHeaderSize()); serverProperties.getMaxHttpHeaderSize());
......
...@@ -64,6 +64,7 @@ import static org.mockito.Mockito.verify; ...@@ -64,6 +64,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb * @author Phillip Webb
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Quinten De Swaef * @author Quinten De Swaef
* @author Venil Noronha
*/ */
public class ServerPropertiesTests { public class ServerPropertiesTests {
...@@ -260,6 +261,30 @@ public class ServerPropertiesTests { ...@@ -260,6 +261,30 @@ public class ServerPropertiesTests {
assertThat(this.properties.getMaxHttpHeaderSize()).isEqualTo(9999); assertThat(this.properties.getMaxHttpHeaderSize()).isEqualTo(9999);
} }
@Test
public void testCustomizePostSize() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.maxHttpPostSize", "9999");
bindProperties(map);
assertThat(this.properties.getMaxHttpPostSize()).isEqualTo(9999);
}
@Test
public void testCustomizeJettyAcceptors() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.jetty.acceptors", "10");
bindProperties(map);
assertThat(this.properties.getJetty().getAcceptors()).isEqualTo(10);
}
@Test
public void testCustomizeJettySelectors() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.jetty.selectors", "10");
bindProperties(map);
assertThat(this.properties.getJetty().getSelectors()).isEqualTo(10);
}
@Test @Test
public void testCustomizeTomcatMinSpareThreads() throws Exception { public void testCustomizeTomcatMinSpareThreads() throws Exception {
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
......
...@@ -218,6 +218,8 @@ content into your application; rather pick only the properties that you need. ...@@ -218,6 +218,8 @@ content into your application; rather pick only the properties that you need.
server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL. server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.
server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR` server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR`
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI. server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
server.jetty.acceptors= # Number of acceptor threads to use.
server.jetty.selectors= # Number of selector threads to use.
server.undertow.accesslog.dir= # Undertow access log directory. server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Enable access log. server.undertow.accesslog.enabled=false # Enable access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs. server.undertow.accesslog.pattern=common # Format pattern for access logs.
......
...@@ -91,6 +91,7 @@ import org.springframework.util.StringUtils; ...@@ -91,6 +91,7 @@ import org.springframework.util.StringUtils;
* @author Andrey Hihlovskiy * @author Andrey Hihlovskiy
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Venil Noronha
* @see #setPort(int) * @see #setPort(int)
* @see #setConfigurations(Collection) * @see #setConfigurations(Collection)
* @see JettyEmbeddedServletContainer * @see JettyEmbeddedServletContainer
...@@ -108,6 +109,16 @@ public class JettyEmbeddedServletContainerFactory ...@@ -108,6 +109,16 @@ public class JettyEmbeddedServletContainerFactory
private boolean useForwardHeaders; private boolean useForwardHeaders;
/**
* The number of acceptor threads to use.
*/
private int acceptors = -1;
/**
* The number of selector threads to use.
*/
private int selectors = -1;
private List<JettyServerCustomizer> jettyServerCustomizers = new ArrayList<JettyServerCustomizer>(); private List<JettyServerCustomizer> jettyServerCustomizers = new ArrayList<JettyServerCustomizer>();
private ResourceLoader resourceLoader; private ResourceLoader resourceLoader;
...@@ -143,7 +154,7 @@ public class JettyEmbeddedServletContainerFactory ...@@ -143,7 +154,7 @@ public class JettyEmbeddedServletContainerFactory
ServletContextInitializer... initializers) { ServletContextInitializer... initializers) {
JettyEmbeddedWebAppContext context = new JettyEmbeddedWebAppContext(); JettyEmbeddedWebAppContext context = new JettyEmbeddedWebAppContext();
int port = (getPort() >= 0 ? getPort() : 0); int port = (getPort() >= 0 ? getPort() : 0);
Server server = new Server(new InetSocketAddress(getAddress(), port)); Server server = createServer(port);
configureWebAppContext(context, initializers); configureWebAppContext(context, initializers);
server.setHandler(addHandlerWrappers(context)); server.setHandler(addHandlerWrappers(context));
this.logger.info("Server initialized with port: " + port); this.logger.info("Server initialized with port: " + port);
...@@ -163,6 +174,21 @@ public class JettyEmbeddedServletContainerFactory ...@@ -163,6 +174,21 @@ public class JettyEmbeddedServletContainerFactory
return getJettyEmbeddedServletContainer(server); return getJettyEmbeddedServletContainer(server);
} }
private Server createServer(int port) {
Server server = new Server();
server.setConnectors(new Connector[] { createConnector(port, server) });
return server;
}
private ServerConnector createConnector(int port, Server server) {
ServerConnector connector = new ServerConnector(server, this.acceptors,
this.selectors);
InetSocketAddress address = new InetSocketAddress(getAddress(), port);
connector.setHost(address.getHostName());
connector.setPort(address.getPort());
return connector;
}
private Handler addHandlerWrappers(Handler handler) { private Handler addHandlerWrappers(Handler handler) {
if (getCompression() != null && getCompression().getEnabled()) { if (getCompression() != null && getCompression().getEnabled()) {
handler = applyWrapper(handler, createGzipHandler()); handler = applyWrapper(handler, createGzipHandler());
...@@ -489,6 +515,24 @@ public class JettyEmbeddedServletContainerFactory ...@@ -489,6 +515,24 @@ public class JettyEmbeddedServletContainerFactory
this.useForwardHeaders = useForwardHeaders; this.useForwardHeaders = useForwardHeaders;
} }
/**
* Set the number of acceptor threads to use.
* @param acceptors the number of acceptor threads to use
* @since 1.4.0
*/
public void setAcceptors(int acceptors) {
this.acceptors = acceptors;
}
/**
* Set the number of selector threads to use.
* @param selectors the number of selector threads to use
* @since 1.4.0
*/
public void setSelectors(int selectors) {
this.selectors = selectors;
}
/** /**
* Sets {@link JettyServerCustomizer}s that will be applied to the {@link Server} * Sets {@link JettyServerCustomizer}s that will be applied to the {@link Server}
* before it is started. Calling this method will replace any existing configurations. * before it is started. Calling this method will replace any existing configurations.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment