2012-04-06 62 views
0

我正在尋找一種方法來通過java以編程方式管理tomcat(在本地主機上)。 我想啓動/停止tomcat並部署WAR。如何通過Java管理Tomcat

任何幫助表示讚賞。

+0

Tomcat是一個Java程序。可能應該只是調用它的Main類,查看腳本以瞭解庫和參數。什麼是真正的問題? – Jayan 2012-04-06 16:48:50

+0

你有機會嘗試春季開機? – wdoering 2015-10-13 17:16:08

回答

1

您可以使用Java Runtime類調用一個bat文件。確保用戶運行java進程有權啓動和停止tomcat。

try{ 
Runtime.getRuntime().exec("c:/program files/tomcat/bin/startup.bat"); 
} catch(IOException e) {System.out.println("exception");} 
0

要以編程方式管理tomcat,您可能需要查看JMX以及Tomcat的插件MBean的功能。

實際上,您可以編寫您自己的基於java的JMX客戶端,以通過RMI與MBean對話,或者可以利用管理器應用程序中的JMX Http Proxy並使用普通的舊http請求來編寫腳本並管理該tomcat實例。

對於JMX和Tomcat 6的一個很好的參考: http://www.datadisk.co.uk/html_docs/java_app/tomcat6/tomcat6_jmx.htm

管理器應用程序和JMX HTTP代理,很好的參考: http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#JMX_Set_command

你應該能夠非常輕鬆地部署和取消部署戰爭。

我不認爲有一個現有的MBean可以讓你關閉tomcat,但是你自己實現一個並且調用System.exit()是相當容易的。

2

您可以運行嵌入在您的應用程序中的Tomcat。

+0

這樣一來,嵌入式tomcat開發起來非常方便,你可能會發現嵌入示例[here](https://github.com/alx3apps/spring-embedded-tomcat) – alexkasko 2012-04-07 14:01:13

0

您可以重新啓動單個Tomcat連接器,即重啓應用程序所在的端口,例如8843。需要這種情況的一種情況是當您通過API獲取簽名證書或者您正在修改您的信任庫時。

下面是我用來在添加/刪除證書後重新啓動tomcat連接器的完整代碼/方法。

public void refreshTrustStore() throws Exception 
    { 
     try 
     { 
      //following line need to be replaced based on where you get your port. It may be passed in as argument 
      String httpsPort = configurationManager.getHttpsPort(); 
      String objectString = "*:type=Connector,port=" + httpsPort + ",*"; 

      final ObjectName objectNameQuery = new ObjectName(objectString); 

      for (final MBeanServer server : MBeanServerFactory.findMBeanServer(null)) 
      { 
       if (server.queryNames(objectNameQuery, null).size() > 0) 
       { 
        MBeanServer mbeanServer = server; 
        ObjectName objectName = (ObjectName) server.queryNames(objectNameQuery, null).toArray()[0]; 

        mbeanServer.invoke(objectName, "stop", null, null); 

        // Polling sleep to reduce delay to safe minimum. 
        // Use currentTimeMillis() over nanoTime() to avoid issues 
        // with migrating threads across sleep() calls. 
        long start = System.currentTimeMillis(); 
        // Maximum of 6 seconds, 3x time required on an idle system. 
        long max_duration = 6000L; 
        long duration = 0L; 
        do 
        { 
         try 
         { 
          Thread.sleep(100); 
         } 
         catch (InterruptedException e) 
         { 
          Thread.currentThread().interrupt(); 
         } 

         duration = (System.currentTimeMillis() - start); 
        } while (duration < max_duration && 
        server.queryNames(objectNameQuery, null).size() > 0); 

       // Use below to get more accurate metrics. 
      String message = "TrustStoreManager TrustStore Stop: took " + duration + "milliseconds"; 
      logger.information(message); 

      mbeanServer.invoke(objectName, "start", null, null); 

      break; 
     } 
    } 
} 
catch (Exception exception) 
{ 
    //Log and throw exception 
    throw exception 
} 

}