2012-07-30 107 views
1

我有一些代碼嵌入Tomcat的,看起來像這樣:當我啓動服務器本身時,爲什麼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沒有啓動監聽任何端口上。我戳了一下,發現連接器仍處於新狀態。

爲什麼Tomcat本身啓動時不啓動它?

stop()和destroy()同樣發生。他們都沒有打電話。

沒有錯誤發生,如果我稍後致電tomcat.getConnector().start(),它似乎成功並使服務可聯繫。

回答

0

由於您已將bindOnInit設置爲false,您必須明確啓動tomcat的連接器才能開始監聽配置的端口。

設置bindOnInit爲true(或者你不需要設置所有的,因爲真正的是默認值)爲Tomcat啓動監聽tomcat.start()

bindOnInit設置決定是否綁定/解除綁定init/destroy或start/stop上的端口。