2014-01-08 164 views
1

我正在開發一個項目,需要一個Web和桌面應用程序。 Web應用程序從我的客戶端接收任務並將其存儲(在數據庫中)。桌面應用程序從數據庫中獲取任務並逐個執行它們。在我的web應用程序中,我正在使用java servlets,web services ...從java桌面應用程序啓動,停止,重新啓動Glassfish服務器

有時我的glassfish服務器(v 3.1.2)凍結或者他變得被阻塞,需要重新啓動,以便他可以繼續正常工作。通過監視他並發現他何時凍結(通過調用引發異常的簡單Web服務方法,也引發異常的簡單http請求等),我可以檢測到這種錯誤。

我想我的桌面應用程序中獲得GlassFish服務器狀態,如果

  1. 「一切正常」,那麼「什麼都不做」
  2. 「服務器關閉」,然後「啓動GlassFish服務器」
  3. 「我發現一個錯誤」,然後‘重新啓動GlassFish服務器’
  4. ‘應用程序退出’,然後‘關閉GlassFish服務器’

是否anyo ne有這個問題,並有任何解決方案。我厭倦了手動重新啓動glassfish服務器。

回答

0

我找到了自己想要分享的解決方案。

當我檢測到Glassfish服務器出現問題時,我重新啓動它。這個解決方案只適用於Linux(如果我發現Windows用戶是simular,我會編輯這個答案)。另外你可能需要在root用戶下的「/ etc/sudoers」中爲你的用戶添加這行代碼,adrian是我的用戶名。

adrian ALL=(ALL:ALL) ALL 

GlassFish的類別:(U需要改變glassfishPath和域名與你)

package es.web.glassfish; 

    import es.os.linux.Konsole; 
    import java.io.IOException; 

    /** 
    * 
    * @author adrian 
    */ 
    public class Glassfish { 

    private final static String glassfishPath = "/home/adrian/glassfish-4.0/"; 
    private final static String domainName = "domain1"; 

    public static String startGlassfishServer() throws IOException, InterruptedException { 
     String command = glassfishPath + "bin/asadmin start-domain "+domainName; 
     return Konsole.executeCommand(command); 
    } 

    public static String stopGlassfishServer() throws IOException, InterruptedException { 
     String command = glassfishPath + "bin/asadmin stop-domain "+domainName; 
     return Konsole.executeCommand(command); 
    } 

    public static String restrartGlassfishServer() throws IOException, InterruptedException { 
     String command = glassfishPath + "bin/asadmin restart-domain "+domainName; 
     return Konsole.executeCommand(command); 
    } 

} 

的Konsole類:

package es.os.linux; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

/** 
* 
* @author adrian 
*/ 
public class Konsole { 

    static Process process; 
    static BufferedReader reader; 

    public static String executeCommand(String command) throws IOException, InterruptedException { 
     String rez = ""; 
     process = Runtime.getRuntime().exec(command); 
     process.waitFor(); 
     reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      rez += line + "#"; 
     } 
     return rez; 
    } 
} 

測試類:

public class test { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args){ 
     try { 
      System.out.println("START"); 
      System.out.println(Glassfish.startGlassfishServer()); 
      System.out.println("RESTART"); 
      System.out.println(Glassfish.restrartGlassfishServer()); 
      System.out.println("STOP"); 
      System.out.println(Glassfish.stopGlassfishServer()); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } catch (InterruptedException ex) { 
      ex.printStackTrace(); 
     } 
    } 

} 

測試班級輸出:

START 
Waiting for domain1 to start ............#Successfully started the domain : domain1#domain Location: /home/adrian/glassfish-4.0/glassfish/domains/domain1#Log File: /home/adrian/glassfish-4.0/glassfish/domains/domain1/logs/server.log#Admin Port: 4848#Command start-domain executed successfully.# 
RESTART 
Successfully restarted the domain#Command restart-domain executed successfully.# 
STOP 
Waiting for the domain to stop #Command stop-domain executed successfully.# 
+0

再次看到我上面的評論。另外,您沒有正確使用Process/Runtime exec API。您正在讀取與執行過程相同的線程中的輸入流。這是在看似隨機執行時發生線程死鎖的好方法。 – NBW

+0

我知道你的回答很有用,但並沒有解決我的問題。該解決方案還可以在停止時從桌面應用程序啓動glassfish服務器。重新啓動命令在服務器關閉時啓動服務器,並在啓動時重新啓動服務器。 – AdrianES

3

我在生產中一次運行Glassfish 3.1.2幾個月沒有問題。我懷疑你看到的凍結是你部署到它的應用程序的問題。

我認爲你最好花時間調查和糾正你的懸掛問題。您是否嘗試過在發生這種情況時使用Glassfish java進程的線程轉儲?

+0

問題是要麼我不能調用webservice方法或http客戶端不會收到任何數據。試了很多東西,只重啓了作品。 – AdrianES

+0

這表明您的應用程序正在捱餓HTTP線程池。同樣,當你遇到這種情況時,你應該進行線程轉儲,這樣你就可以看到發生了什麼。 – NBW

+0

我增加了http線程池,從那時起我沒有任何問題,但是如果有時會發生,我想重新啓動服務器後,我發現問題出在哪裏。 – AdrianES

相關問題