Fixes #55: stop() connector to unbind socket
The `Tomcat.start()` has to happen to initialize the `ServletContext` but we can immediately stop the connector and then restart it when the context is finished refreshing. Seems to make curl fail quickly if an app is slow to start.
This commit is contained in:
@@ -27,7 +27,7 @@ public class SampleController {
|
||||
|
||||
@Autowired
|
||||
private HelloWorldService helloWorldService;
|
||||
|
||||
|
||||
@RequestMapping("/")
|
||||
@ResponseBody
|
||||
public String helloWorld() {
|
||||
|
||||
@@ -55,6 +55,16 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
private synchronized void initialize() throws EmbeddedServletContainerException {
|
||||
try {
|
||||
this.tomcat.start();
|
||||
try {
|
||||
// Allow the server to start so the ServletContext is available, but stop
|
||||
// the connector to prevent requests from being handled before the Spring
|
||||
// context is ready:
|
||||
Connector connector = this.tomcat.getConnector();
|
||||
connector.getProtocolHandler().stop();
|
||||
}
|
||||
catch (Exception e) {
|
||||
this.logger.error("Cannot pause connector: ", e);
|
||||
}
|
||||
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
|
||||
// blocking non-daemon to stop immediate shutdown
|
||||
Thread awaitThread = new Thread("container-" + (containerCounter++)) {
|
||||
@@ -77,7 +87,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
Connector connector = this.tomcat.getConnector();
|
||||
if (connector != null) {
|
||||
try {
|
||||
connector.getProtocolHandler().resume();
|
||||
connector.getProtocolHandler().start();
|
||||
}
|
||||
catch (Exception e) {
|
||||
this.logger.error("Cannot start connector: ", e);
|
||||
|
||||
@@ -131,15 +131,6 @@ public class TomcatEmbeddedServletContainerFactory extends
|
||||
customizeConnector(connector);
|
||||
tomcat.getService().addConnector(connector);
|
||||
tomcat.setConnector(connector);
|
||||
try {
|
||||
// Allow the server to start so the ServletContext is available, but stop the
|
||||
// connector to prevent requests from being handled before the Spring context
|
||||
// is ready:
|
||||
connector.getProtocolHandler().pause();
|
||||
}
|
||||
catch (Exception e) {
|
||||
this.logger.error("Cannot pause connector: ", e);
|
||||
}
|
||||
tomcat.getHost().setAutoDeploy(false);
|
||||
tomcat.getEngine().setBackgroundProcessorDelay(-1);
|
||||
|
||||
@@ -203,10 +194,15 @@ public class TomcatEmbeddedServletContainerFactory extends
|
||||
// Needs to be protected so it can be used by subclasses
|
||||
protected void customizeConnector(Connector connector) {
|
||||
connector.setPort(getPort());
|
||||
if (connector.getProtocolHandler() instanceof AbstractProtocol
|
||||
&& getAddress() != null) {
|
||||
((AbstractProtocol) connector.getProtocolHandler()).setAddress(getAddress());
|
||||
if (connector.getProtocolHandler() instanceof AbstractProtocol) {
|
||||
if (getAddress() != null) {
|
||||
((AbstractProtocol) connector.getProtocolHandler())
|
||||
.setAddress(getAddress());
|
||||
}
|
||||
}
|
||||
// If ApplicationContext is slow to start we want Tomcat not to bind to the socket
|
||||
// prematurely...
|
||||
connector.setProperty("bindOnInit", "false");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user