我找到了自己想要分享的解决方案。
当我检测到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