Commit 757aa647 authored by Michael K. Werle's avatar Michael K. Werle Committed by Phillip Webb

Ensure web containers are stopped after close

Update `EmbeddedServletContainer` implementations to ensure that stop
can be called even if start has not. This allows servers that are
partially started during `initialize()` to still be shut down.

This commit fixes a regression caused by commit 0af53b36.

See gh-8036
Fixes gh-8224
Closes gh-8227
parent c06a9771
...@@ -205,9 +205,6 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer { ...@@ -205,9 +205,6 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer {
@Override @Override
public void stop() { public void stop() {
synchronized (this.monitor) { synchronized (this.monitor) {
if (!this.started) {
return;
}
this.started = false; this.started = false;
try { try {
this.server.stop(); this.server.stop();
......
...@@ -279,9 +279,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer ...@@ -279,9 +279,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
@Override @Override
public void stop() throws EmbeddedServletContainerException { public void stop() throws EmbeddedServletContainerException {
synchronized (this.monitor) { synchronized (this.monitor) {
if (!this.started) {
return;
}
try { try {
this.started = false; this.started = false;
try { try {
......
...@@ -143,6 +143,16 @@ public class JettyEmbeddedServletContainerFactoryTests ...@@ -143,6 +143,16 @@ public class JettyEmbeddedServletContainerFactoryTests
.isEmpty(); .isEmpty();
} }
@Test
public void stopNoStart() throws Exception {
JettyEmbeddedServletContainerFactory factory = getFactory();
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.stop();
Server server = ((JettyEmbeddedServletContainer) this.container).getServer();
assertThat(server.isStopped()).isTrue();
}
@Override @Override
protected void addConnector(final int port, protected void addConnector(final int port,
AbstractEmbeddedServletContainerFactory factory) { AbstractEmbeddedServletContainerFactory factory) {
......
...@@ -352,6 +352,16 @@ public class TomcatEmbeddedServletContainerFactoryTests ...@@ -352,6 +352,16 @@ public class TomcatEmbeddedServletContainerFactoryTests
.doesNotContain("appears to have started a thread named [main]"); .doesNotContain("appears to have started a thread named [main]");
} }
@Test
public void stopNoStart() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.stop();
Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat();
assertThat(tomcat.getServer().getState()).isSameAs(LifecycleState.DESTROYED);
}
@Override @Override
protected void addConnector(int port, protected void addConnector(int port,
AbstractEmbeddedServletContainerFactory factory) { AbstractEmbeddedServletContainerFactory factory) {
......
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