I have code to embed Tomcat which looks like this:
public void start()
{
final Tomcat tomcat = createTomcat();
try
{
tomcat.start();
}
catch (Exception e)
{
throw new RuntimeException("Failed to start web server", e);
}
this.tomcat = tomcat;
}
private Tomcat createTomcat()
{
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir(FileUtils.getTempDirectory().getAbsolutePath());
tomcat.setConnector(createConnector());
// ... eliding the webapp, session, access log setup ...
return tomcat;
}
private Connector createConnector()
{
Connector connector = new Connector();
connector.setPort(context.getWebServerPort());
connector.setScheme("https");
connector.setSecure(true);
//prepareKeyStore(context.getListeningHost());
connector.setAttribute("address", context.getListeningAddress());
connector.setAttribute("SSLEnabled", true);
// Bind on start() instead of init() so that the port is closed faster on shutdown
// I still have another problem where stop() seems to return before shutdown is
// truly complete!
connector.setAttribute("bindOnInit", false);
connector.setAttribute("keystoreFile", "/my/keystore");
connector.setAttribute("keystorePass", "password");
connector.setAttribute("clientAuth", "false");
connector.setAttribute("sslProtocol", "TLS");
connector.setAttribute("keyAlias", context.getListeningHost());
connector.setAttribute("keyPass", "password");
return connector;
}
When this runs, Tomcat doesn't start listening on any port. I poked around and found that the connector was still in state NEW.
Why doesn't Tomcat start it when I start Tomcat itself?
The same occurs on stop() and destroy(). Neither of them appear to call it.
No errors occur, and if I later call tomcat.getConnector().start()
, it appears to succeed and make the service contactable.