Skip to content

Commit a192970

Browse files
committed
Merge pull request spring-projects#5324 from Henri Kerola
* spring-projectsgh-5324: Polish "Allow Jetty's ThreadPool to be customized" (spring-projectsgh-5324) Allow Jetty's ThreadPool to be customized
2 parents 2da7799 + acda000 commit a192970

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.eclipse.jetty.util.resource.JarResource;
5757
import org.eclipse.jetty.util.resource.Resource;
5858
import org.eclipse.jetty.util.ssl.SslContextFactory;
59+
import org.eclipse.jetty.util.thread.ThreadPool;
5960
import org.eclipse.jetty.webapp.AbstractConfiguration;
6061
import org.eclipse.jetty.webapp.Configuration;
6162
import org.eclipse.jetty.webapp.WebAppContext;
@@ -92,6 +93,7 @@
9293
* @author Andy Wilkinson
9394
* @author Eddú Meléndez
9495
* @author Venil Noronha
96+
* @author Henri Kerola
9597
* @see #setPort(int)
9698
* @see #setConfigurations(Collection)
9799
* @see JettyEmbeddedServletContainer
@@ -125,6 +127,8 @@ public class JettyEmbeddedServletContainerFactory
125127

126128
private ResourceLoader resourceLoader;
127129

130+
private ThreadPool threadPool;
131+
128132
/**
129133
* Create a new {@link JettyEmbeddedServletContainerFactory} instance.
130134
*/
@@ -178,7 +182,13 @@ public EmbeddedServletContainer getEmbeddedServletContainer(
178182
}
179183

180184
private Server createServer(InetSocketAddress address) {
181-
Server server = new Server();
185+
Server server;
186+
if (ClassUtils.hasConstructor(Server.class, ThreadPool.class)) {
187+
server = new Jetty9ServerFactory().createServer(getThreadPool());
188+
}
189+
else {
190+
server = new Jetty8ServerFactory().createServer(getThreadPool());
191+
}
182192
server.setConnectors(new Connector[] { createConnector(address, server) });
183193
return server;
184194
}
@@ -596,6 +606,24 @@ public void addConfigurations(Configuration... configurations) {
596606
this.configurations.addAll(Arrays.asList(configurations));
597607
}
598608

609+
/**
610+
* Returns a Jetty {@link ThreadPool} that should be used by the {@link Server}.
611+
* @return a Jetty {@link ThreadPool} or {@code null}
612+
*/
613+
public ThreadPool getThreadPool() {
614+
return this.threadPool;
615+
}
616+
617+
/**
618+
* Set a Jetty {@link ThreadPool} that should be used by the {@link Server}.
619+
* If set to {@code null} (default), the {@link Server} creates
620+
* a {@link ThreadPool} implicitly.
621+
* @param threadPool a Jetty ThreadPool to be used
622+
*/
623+
public void setThreadPool(ThreadPool threadPool) {
624+
this.threadPool = threadPool;
625+
}
626+
599627
private void addJettyErrorPages(ErrorHandler errorHandler,
600628
Collection<ErrorPage> errorPages) {
601629
if (errorHandler instanceof ErrorPageErrorHandler) {
@@ -858,4 +886,36 @@ public AbstractConnector createConnector(Server server, InetSocketAddress addres
858886

859887
}
860888

889+
private interface ServerFactory {
890+
891+
Server createServer(ThreadPool threadPool);
892+
893+
}
894+
895+
private static class Jetty8ServerFactory implements ServerFactory {
896+
897+
@Override
898+
public Server createServer(ThreadPool threadPool) {
899+
Server server = new Server();
900+
try {
901+
ReflectionUtils.findMethod(Server.class, "setThreadPool", ThreadPool.class)
902+
.invoke(server, threadPool);
903+
}
904+
catch (Exception e) {
905+
throw new RuntimeException("Failed to configure Jetty 8 ThreadPool", e);
906+
}
907+
return server;
908+
}
909+
910+
}
911+
912+
private static class Jetty9ServerFactory implements ServerFactory {
913+
914+
@Override
915+
public Server createServer(ThreadPool threadPool) {
916+
return new Server(threadPool);
917+
}
918+
919+
}
920+
861921
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.eclipse.jetty.server.handler.HandlerCollection;
3535
import org.eclipse.jetty.server.handler.HandlerWrapper;
3636
import org.eclipse.jetty.servlet.ServletHolder;
37+
import org.eclipse.jetty.util.thread.ThreadPool;
3738
import org.eclipse.jetty.webapp.Configuration;
3839
import org.eclipse.jetty.webapp.WebAppContext;
3940
import org.junit.Test;
@@ -58,6 +59,7 @@
5859
* @author Phillip Webb
5960
* @author Dave Syer
6061
* @author Andy Wilkinson
62+
* @author Henri Kerola
6163
*/
6264
public class JettyEmbeddedServletContainerFactoryTests
6365
extends AbstractEmbeddedServletContainerFactoryTests {
@@ -256,6 +258,26 @@ public void useForwardHeaders() throws Exception {
256258
assertForwardHeaderIsUsed(factory);
257259
}
258260

261+
@Test
262+
public void defaultThreadPool() throws Exception {
263+
JettyEmbeddedServletContainerFactory factory = getFactory();
264+
factory.setThreadPool(null);
265+
assertThat(factory.getThreadPool()).isNull();
266+
JettyEmbeddedServletContainer servletContainer = (JettyEmbeddedServletContainer) factory
267+
.getEmbeddedServletContainer();
268+
assertThat(servletContainer.getServer().getThreadPool()).isNotNull();
269+
}
270+
271+
@Test
272+
public void customThreadPool() throws Exception {
273+
JettyEmbeddedServletContainerFactory factory = getFactory();
274+
ThreadPool threadPool = mock(ThreadPool.class);
275+
factory.setThreadPool(threadPool);
276+
JettyEmbeddedServletContainer servletContainer = (JettyEmbeddedServletContainer) factory
277+
.getEmbeddedServletContainer();
278+
assertThat(servletContainer.getServer().getThreadPool()).isSameAs(threadPool);
279+
}
280+
259281
@Override
260282
@SuppressWarnings("serial")
261283
// Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646

0 commit comments

Comments
 (0)