简体   繁体   中英

Why doesn't Tomcat start the connector when I start the server itself?

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.

Since you have set bindOnInit to false, you have to explicitly start the connector for tomcat to start listening to the configured port.

Set bindOnInit to true (or you need not set it at all, as true is the default value) for the tomcat to start listening on tomcat.start()

bindOnInit setting decides whether to bind/unbind the ports on init/destroy or start/stop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM