Polish embedded tomcat classes
Extract some methods to aid readability.
This commit is contained in:
parent
aed243fb88
commit
f7b7b1c8cb
|
|
@ -37,6 +37,7 @@ public class ServletContextInitializerLifecycleListener implements LifecycleList
|
||||||
.getLog(ServletContextInitializerLifecycleListener.class);
|
.getLog(ServletContextInitializerLifecycleListener.class);
|
||||||
|
|
||||||
private final ServletContextInitializer[] initializers;
|
private final ServletContextInitializer[] initializers;
|
||||||
|
|
||||||
private Exception startUpException;
|
private Exception startUpException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@ import org.apache.catalina.Container;
|
||||||
import org.apache.catalina.Engine;
|
import org.apache.catalina.Engine;
|
||||||
import org.apache.catalina.LifecycleException;
|
import org.apache.catalina.LifecycleException;
|
||||||
import org.apache.catalina.LifecycleState;
|
import org.apache.catalina.LifecycleState;
|
||||||
import org.apache.catalina.Server;
|
|
||||||
import org.apache.catalina.Service;
|
import org.apache.catalina.Service;
|
||||||
import org.apache.catalina.connector.Connector;
|
import org.apache.catalina.connector.Connector;
|
||||||
import org.apache.catalina.startup.Tomcat;
|
import org.apache.catalina.startup.Tomcat;
|
||||||
|
|
@ -77,46 +76,21 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||||
|
|
||||||
private synchronized void initialize() throws EmbeddedServletContainerException {
|
private synchronized void initialize() throws EmbeddedServletContainerException {
|
||||||
try {
|
try {
|
||||||
Server server = this.tomcat.getServer();
|
addInstanceIdToEngineName();
|
||||||
int instanceId = containerCounter.incrementAndGet();
|
|
||||||
if (instanceId > 0) {
|
|
||||||
Engine engine = this.tomcat.getEngine();
|
|
||||||
engine.setName(engine.getName() + "-" + instanceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove service connectors to that protocol binding doesn't happen yet
|
// Remove service connectors to that protocol binding doesn't happen yet
|
||||||
for (Service service : server.findServices()) {
|
removeServiceConnectors();
|
||||||
Connector[] connectors = service.findConnectors().clone();
|
|
||||||
this.serviceConnectors.put(service, connectors);
|
|
||||||
for (Connector connector : connectors) {
|
|
||||||
service.removeConnector(connector);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the server to trigger initialization listeners
|
// Start the server to trigger initialization listeners
|
||||||
this.tomcat.start();
|
this.tomcat.start();
|
||||||
|
|
||||||
Container[] children = this.tomcat.getHost().findChildren();
|
// We can re-throw failure exception directly in the main thread
|
||||||
for (Container container : children) {
|
rethrowDeferredStartupExceptions();
|
||||||
if (container instanceof TomcatEmbeddedContext) {
|
|
||||||
Exception exception = ((TomcatEmbeddedContext) container)
|
|
||||||
.getStarter().getStartUpException();
|
|
||||||
if (exception != null) {
|
|
||||||
throw exception;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
|
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
|
||||||
// blocking non-daemon to stop immediate shutdown
|
// blocking non-daemon to stop immediate shutdown
|
||||||
Thread awaitThread = new Thread("container-" + (containerCounter.get())) {
|
startDaemonAwaitThread();
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
TomcatEmbeddedServletContainer.this.tomcat.getServer().await();
|
|
||||||
};
|
|
||||||
};
|
|
||||||
awaitThread.setDaemon(false);
|
|
||||||
awaitThread.start();
|
|
||||||
if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) {
|
if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) {
|
||||||
this.tomcat.stop();
|
this.tomcat.stop();
|
||||||
throw new IllegalStateException("Tomcat connector in failed state");
|
throw new IllegalStateException("Tomcat connector in failed state");
|
||||||
|
|
@ -128,9 +102,58 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addInstanceIdToEngineName() {
|
||||||
|
int instanceId = containerCounter.incrementAndGet();
|
||||||
|
if (instanceId > 0) {
|
||||||
|
Engine engine = this.tomcat.getEngine();
|
||||||
|
engine.setName(engine.getName() + "-" + instanceId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeServiceConnectors() {
|
||||||
|
for (Service service : this.tomcat.getServer().findServices()) {
|
||||||
|
Connector[] connectors = service.findConnectors().clone();
|
||||||
|
this.serviceConnectors.put(service, connectors);
|
||||||
|
for (Connector connector : connectors) {
|
||||||
|
service.removeConnector(connector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void rethrowDeferredStartupExceptions() throws Exception {
|
||||||
|
Container[] children = this.tomcat.getHost().findChildren();
|
||||||
|
for (Container container : children) {
|
||||||
|
if (container instanceof TomcatEmbeddedContext) {
|
||||||
|
Exception exception = ((TomcatEmbeddedContext) container).getStarter()
|
||||||
|
.getStartUpException();
|
||||||
|
if (exception != null) {
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startDaemonAwaitThread() {
|
||||||
|
Thread awaitThread = new Thread("container-" + (containerCounter.get())) {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
TomcatEmbeddedServletContainer.this.tomcat.getServer().await();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
awaitThread.setDaemon(false);
|
||||||
|
awaitThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() throws EmbeddedServletContainerException {
|
public void start() throws EmbeddedServletContainerException {
|
||||||
// Add the previously removed connectors (also starting them)
|
addPreviouslyRemovedConnectors();
|
||||||
|
Connector connector = this.tomcat.getConnector();
|
||||||
|
if (connector != null && this.autoStart) {
|
||||||
|
startConnector(connector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addPreviouslyRemovedConnectors() {
|
||||||
Service[] services = this.tomcat.getServer().findServices();
|
Service[] services = this.tomcat.getServer().findServices();
|
||||||
for (Service service : services) {
|
for (Service service : services) {
|
||||||
Connector[] connectors = this.serviceConnectors.get(service);
|
Connector[] connectors = this.serviceConnectors.get(service);
|
||||||
|
|
@ -138,21 +161,30 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||||
for (Connector connector : connectors) {
|
for (Connector connector : connectors) {
|
||||||
service.addConnector(connector);
|
service.addConnector(connector);
|
||||||
if (!this.autoStart) {
|
if (!this.autoStart) {
|
||||||
unbind(connector);
|
stopProtocolHandler(connector);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.serviceConnectors.remove(service);
|
this.serviceConnectors.remove(service);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Connector connector = this.tomcat.getConnector();
|
}
|
||||||
if (connector != null && this.autoStart) {
|
|
||||||
|
private void stopProtocolHandler(Connector connector) {
|
||||||
|
try {
|
||||||
|
connector.getProtocolHandler().stop();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
this.logger.error("Cannot pause connector: ", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startConnector(Connector connector) {
|
||||||
try {
|
try {
|
||||||
for (Container child : this.tomcat.getHost().findChildren()) {
|
for (Container child : this.tomcat.getHost().findChildren()) {
|
||||||
if (child instanceof TomcatEmbeddedContext) {
|
if (child instanceof TomcatEmbeddedContext) {
|
||||||
((TomcatEmbeddedContext) child).deferredLoadOnStartup();
|
((TomcatEmbeddedContext) child).deferredLoadOnStartup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
connector.getProtocolHandler().start();
|
|
||||||
logPorts();
|
logPorts();
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
|
|
@ -161,16 +193,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||||
"Unable to start embedded Tomcat connectors", ex);
|
"Unable to start embedded Tomcat connectors", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void unbind(Connector connector) {
|
|
||||||
try {
|
|
||||||
connector.getProtocolHandler().stop();
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
this.logger.error("Cannot pause connector: ", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<Service, Connector[]> getServiceConnectors() {
|
Map<Service, Connector[]> getServiceConnectors() {
|
||||||
return this.serviceConnectors;
|
return this.serviceConnectors;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue