[英]Start, stop, restart Glassfish Server from java desktop application
我正在開發一個需要展示網絡和桌面應用程序的項目。 Web應用程序從我的客戶端接收任務並存儲它們(在數據庫中)。 桌面應用程序從數據庫中獲取任務並逐個執行。 在我的Web應用程序中,我正在使用java servlet,Web服務......
有時我的glassfish服務器(v 3.1.2)會凍結或被阻塞,需要重新啟動才能繼續正常工作。 我可以通過監視他並發現他何時凍結(通過調用拋出異常的簡單Web服務方法,也引發異常的簡單http請求等)來檢測這種錯誤。
我希望我的桌面應用程序獲得Glassfish服務器狀態,如果
有沒有人有這個問題,並有任何解決方案。 我厭倦了手動重啟glassfish服務器。
我在生產中運行Glassfish 3.1.2幾個月沒有問題。 我懷疑您所看到的凍結是您部署到它的應用程序的問題。
我認為你最好花時間調查和糾正懸而未決的問題。 當發生這種情況時,您是否嘗試過使用Glassfish java進程的線程轉儲?
我找到了自己想要分享的解決方案。
當我檢測到Glassfish服務器出現問題時,我重啟它。 此解決方案僅適用於Linux(如果我找到Windows用戶的simular,我將編輯此答案)。 此外,您可能需要在root用戶下的“/ etc / sudoers”中為您的用戶添加此行,adrian是我的用戶名。
adrian ALL=(ALL:ALL) ALL
GlassFish類:(你需要用你的名字改變glassfishPath和domainName)
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.#
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.