|
56 | 56 | import org.eclipse.jetty.util.resource.JarResource;
|
57 | 57 | import org.eclipse.jetty.util.resource.Resource;
|
58 | 58 | import org.eclipse.jetty.util.ssl.SslContextFactory;
|
| 59 | +import org.eclipse.jetty.util.thread.ThreadPool; |
59 | 60 | import org.eclipse.jetty.webapp.AbstractConfiguration;
|
60 | 61 | import org.eclipse.jetty.webapp.Configuration;
|
61 | 62 | import org.eclipse.jetty.webapp.WebAppContext;
|
|
92 | 93 | * @author Andy Wilkinson
|
93 | 94 | * @author Eddú Meléndez
|
94 | 95 | * @author Venil Noronha
|
| 96 | + * @author Henri Kerola |
95 | 97 | * @see #setPort(int)
|
96 | 98 | * @see #setConfigurations(Collection)
|
97 | 99 | * @see JettyEmbeddedServletContainer
|
@@ -125,6 +127,8 @@ public class JettyEmbeddedServletContainerFactory
|
125 | 127 |
|
126 | 128 | private ResourceLoader resourceLoader;
|
127 | 129 |
|
| 130 | + private ThreadPool threadPool; |
| 131 | + |
128 | 132 | /**
|
129 | 133 | * Create a new {@link JettyEmbeddedServletContainerFactory} instance.
|
130 | 134 | */
|
@@ -178,7 +182,13 @@ public EmbeddedServletContainer getEmbeddedServletContainer(
|
178 | 182 | }
|
179 | 183 |
|
180 | 184 | 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 | + } |
182 | 192 | server.setConnectors(new Connector[] { createConnector(address, server) });
|
183 | 193 | return server;
|
184 | 194 | }
|
@@ -596,6 +606,24 @@ public void addConfigurations(Configuration... configurations) {
|
596 | 606 | this.configurations.addAll(Arrays.asList(configurations));
|
597 | 607 | }
|
598 | 608 |
|
| 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 | + |
599 | 627 | private void addJettyErrorPages(ErrorHandler errorHandler,
|
600 | 628 | Collection<ErrorPage> errorPages) {
|
601 | 629 | if (errorHandler instanceof ErrorPageErrorHandler) {
|
@@ -858,4 +886,36 @@ public AbstractConnector createConnector(Server server, InetSocketAddress addres
|
858 | 886 |
|
859 | 887 | }
|
860 | 888 |
|
| 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 | + |
861 | 921 | }
|
0 commit comments