Add instance id suffix to Tomcat Engine name

Fixes gh-160
pull/160/merge
Dave Syer 11 years ago
parent cf53b76430
commit 76ea99ad0b

@ -16,6 +16,9 @@
package org.springframework.boot.context.embedded.tomcat;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.catalina.Engine;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.connector.Connector;
@ -39,7 +42,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
private final Log logger = LogFactory.getLog(TomcatEmbeddedServletContainer.class);
private static int containerCounter = 0;
private static AtomicInteger containerCounter = new AtomicInteger(-1);
private final Tomcat tomcat;
@ -66,6 +69,11 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
private synchronized void initialize() throws EmbeddedServletContainerException {
try {
int instanceId = containerCounter.incrementAndGet();
if (instanceId > 0) {
Engine engine = this.tomcat.getEngine();
engine.setName(engine.getName() + "-" + instanceId);
}
this.tomcat.start();
try {
// Allow the server to start so the ServletContext is available, but stop
@ -79,7 +87,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
}
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
// blocking non-daemon to stop immediate shutdown
Thread awaitThread = new Thread("container-" + (containerCounter++)) {
Thread awaitThread = new Thread("container-" + (containerCounter.get())) {
@Override
public void run() {
TomcatEmbeddedServletContainer.this.tomcat.getServer().await();
@ -129,6 +137,9 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
throw new EmbeddedServletContainerException("Unable to stop embedded Tomcat",
ex);
}
finally {
containerCounter.decrementAndGet();
}
}
@Override

@ -30,6 +30,7 @@ import org.mockito.InOrder;
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
@ -52,6 +53,20 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
return new TomcatEmbeddedServletContainerFactory();
}
// JMX MBean names clash if you get more than one Engine with the same name...
@Test
public void tomcatEngineNames() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
this.container = factory.getEmbeddedServletContainer();
factory.setPort(8081);
TomcatEmbeddedServletContainer container2 = (TomcatEmbeddedServletContainer) factory
.getEmbeddedServletContainer();
assertEquals("Tomcat", ((TomcatEmbeddedServletContainer) this.container)
.getTomcat().getEngine().getName());
assertEquals("Tomcat-1", container2.getTomcat().getEngine().getName());
container2.stop();
}
@Test
public void tomcatListeners() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();

Loading…
Cancel
Save