我找到了自己想要分享的解決方案。
當我檢測到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.#
再次看到我上面的評論。另外,您沒有正確使用Process/Runtime exec API。您正在讀取與執行過程相同的線程中的輸入流。這是在看似隨機執行時發生線程死鎖的好方法。 – NBW
我知道你的回答很有用,但並沒有解決我的問題。該解決方案還可以在停止時從桌面應用程序啓動glassfish服務器。重新啓動命令在服務器關閉時啓動服務器,並在啓動時重新啓動服務器。 – AdrianES