Skip to content

Commit bd577f1

Browse files
committed
Rework Jetty startup so connectors are only started once
Previously the server was started to make the ServletContext available, then, to prevent requests from being handled before the application context had been started, the connectors were stopped. Once application context startup had completed, the connectors were then started again. In addition to being somewhat inefficient, this caused problems on FreeBSD where stopping the connector didn't free up the port quickly enough for the subsequent start to then be able to bind to it. This commit updates the Jetty startup logic to be closer to the logic that's used for Tomcat. Before the server is started, the configured connectors are cached and then removed. The server is then started without any connectors. Once application context startup has completed, the connectors are reinstated and started. Fixes #968
1 parent 8c15b13 commit bd577f1

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer {
4444

4545
private final boolean autoStart;
4646

47+
private Connector[] connectors;
48+
4749
/**
4850
* Create a new {@link JettyEmbeddedServletContainer} instance.
4951
* @param server the underlying Jetty server
@@ -65,14 +67,13 @@ public JettyEmbeddedServletContainer(Server server, boolean autoStart) {
6567

6668
private synchronized void initialize() {
6769
try {
70+
// Cache and clear the connectors to prevent requests being handled before
71+
// the application context is ready
72+
this.connectors = this.server.getConnectors();
73+
this.server.setConnectors(null);
74+
75+
// Start the server so that the ServletContext is available
6876
this.server.start();
69-
// Start the server so the ServletContext is available, but stop the
70-
// connectors to prevent requests from being handled before the Spring context
71-
// is ready:
72-
Connector[] connectors = this.server.getConnectors();
73-
for (Connector connector : connectors) {
74-
connector.stop();
75-
}
7677
}
7778
catch (Exception ex) {
7879
try {
@@ -88,6 +89,8 @@ private synchronized void initialize() {
8889

8990
@Override
9091
public void start() throws EmbeddedServletContainerException {
92+
this.server.setConnectors(this.connectors);
93+
9194
if (!this.autoStart) {
9295
return;
9396
}

0 commit comments

Comments
 (0)