Merge pull request #8227 from mkw/gh-8224

* pr/8227:
  Refine engine counter logic
  Polish web containers stop contribution
  Ensure web containers are stopped after close
This commit is contained in:
Phillip Webb 2017-03-01 23:30:40 -08:00
commit fded87225c
5 changed files with 60 additions and 37 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

@ -90,28 +90,34 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
synchronized (this.monitor) {
try {
addInstanceIdToEngineName();
// Remove service connectors to that protocol binding doesn't happen yet
removeServiceConnectors();
// Start the server to trigger initialization listeners
this.tomcat.start();
// We can re-throw failure exception directly in the main thread
rethrowDeferredStartupExceptions();
Context context = findContext();
try {
ContextBindings.bindClassLoader(context, getNamingToken(context),
getClass().getClassLoader());
}
catch (NamingException ex) {
// Naming is not enabled. Continue
}
// Remove service connectors to that protocol binding doesn't happen
// yet
removeServiceConnectors();
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
// blocking non-daemon to stop immediate shutdown
startDaemonAwaitThread();
// Start the server to trigger initialization listeners
this.tomcat.start();
// We can re-throw failure exception directly in the main thread
rethrowDeferredStartupExceptions();
Context context = findContext();
try {
ContextBindings.bindClassLoader(context, getNamingToken(context),
getClass().getClassLoader());
}
catch (NamingException ex) {
// Naming is not enabled. Continue
}
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
// blocking non-daemon to stop immediate shutdown
startDaemonAwaitThread();
}
catch (Exception ex) {
containerCounter.decrementAndGet();
throw ex;
}
}
catch (Exception ex) {
throw new EmbeddedServletContainerException(
@ -279,9 +285,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
@Override
public void stop() throws EmbeddedServletContainerException {
synchronized (this.monitor) {
if (!this.started) {
return;
}
boolean wasStarted = this.started;
try {
this.started = false;
try {
@ -297,7 +301,9 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
"Unable to stop embedded Tomcat", ex);
}
finally {
containerCounter.decrementAndGet();
if (wasStarted) {
containerCounter.decrementAndGet();
}
}
}
}

View File

@ -170,6 +170,16 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
assertThat(this.output.toString()).containsOnlyOnce("started on port");
}
@Test
public void stopCalledTwice() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
this.container.stop();
this.container.stop();
}
@Test
public void emptyServerWhenPortIsMinusOne() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
@ -311,16 +321,6 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
getFactory().setContextPath("/");
}
@Test
public void doubleStop() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
this.container.stop();
this.container.stop();
}
@Test
public void multipleConfigurations() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();

View File

@ -143,6 +143,16 @@ public class JettyEmbeddedServletContainerFactoryTests
.isEmpty();
}
@Test
public void stopCalledWithoutStart() 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 stopCalledWithoutStart() 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) {