1

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.

4

1 回答 1

0

由于您已将 bindOnInit 设置为 false,因此您必须显式启动连接器,以便 tomcat 开始侦听配置的端口。

将 bindOnInit 设置为 true (或者您根本不需要设置它,因为 true 是默认值),以便 tomcat 开始侦听 tomcat.start()

bindOnInit 设置决定是否在 init/destroy 或 start/stop 上绑定/取消绑定端口。

于 2013-08-13T15:14:02.177 回答