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 0af53b361f.

See gh-8036
Fixes gh-8224
Closes gh-8227
This commit is contained in:
Michael K. Werle 2017-02-07 17:20:06 -06:00 committed by Phillip Webb
parent c06a9771c2
commit 757aa647cf
4 changed files with 20 additions and 6 deletions

View File

@ -205,9 +205,6 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer {
@Override
public void stop() {
synchronized (this.monitor) {
if (!this.started) {
return;
}
this.started = false;
try {
this.server.stop();

View File

@ -279,9 +279,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
@Override
public void stop() throws EmbeddedServletContainerException {
synchronized (this.monitor) {
if (!this.started) {
return;
}
try {
this.started = false;
try {

View File

@ -143,6 +143,16 @@ public class JettyEmbeddedServletContainerFactoryTests
.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
protected void addConnector(final int port,
AbstractEmbeddedServletContainerFactory factory) {

View File

@ -352,6 +352,16 @@ public class TomcatEmbeddedServletContainerFactoryTests
.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
protected void addConnector(int port,
AbstractEmbeddedServletContainerFactory factory) {