为什么Tomcat在启动服务器时没有启动连接器?

时间:2012-07-30 05:25:52

标签: java tomcat connector

我有嵌入Tomcat的代码,如下所示:

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;
}

当它运行时,Tomcat不会开始侦听任何端口。我戳了一下,发现连接器仍处于NEW状态。

为什么Tomcat在启动Tomcat时没有启动它?

在stop()和destroy()上也是如此。他们俩似乎都没有打电话。

没有错误发生,如果我稍后调用tomcat.getConnector().start(),它似乎成功并使服务可以联系。

1 个答案:

答案 0 :(得分:0)

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

将tomO开始侦听tomcat.start()

将bindOnInit设置为true(或者根本不需要设置它,因为true是默认值)

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