项目使用 jboss 5.0.1 GA版本,最近总是出现系统 cpu 400%,内存耗尽的问题,查看jboss日志也没有发现什么问题。
无奈之举,想写一个脚本,每周日的晚上 23 点重启jboss服务,可是写了半天,在windows下都没有成功,请大家查看下,谢谢!
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.Date;
import java.io.File;
import java.text.SimpleDateFormat;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
public class RestartjbossService {
private static String separator = File.separator;
private static String filePath = "d:"+separator+"";
public static void main(String[] args) throws InterruptedException {
String cmd1 = "cmd.exe ";// "sh ";//
String cmd2 = "c: ";// "-c ";//
//JBOSS 位置
String cmd31 = "C:"+separator+"jboss-5.0.0.CR2"+separator+"bin"+separator+"shutdown.bat -S";// "/home/posapp/jboss-5.1.0.GA/bin/shutdown.sh -S ";//
String cmd33 = "C:"+separator+"jboss-5.0.0.CR2"+separator+"bin"+separator+"run.bat -b 0.0.0.0 & ";
boolean isRestart = false;
Date date = new Date();
int hours = date.getHours();
if (hours == 14) {
isRestart = true;
}
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
while (isRestart) {
try {
String[] cmd = new String[3];
Date start = new Date();
println(sim.format(start) + ":停止服务...........");
cmd[0] = cmd1;
cmd[1] = cmd2;
cmd[2] = cmd31;
Runtime rt = Runtime.getRuntime();
println("Execing " + cmd[0] + "" + cmd[1] + "" + cmd[2]);
Process proc = rt.exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getInputStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
Thread.sleep(1000);
println("启动服务....... ");
cmd[0] = cmd1;
cmd[1] = cmd2;
cmd[2] = cmd33;
rt = Runtime.getRuntime();
println("Execing " + cmd[0] + "=" + cmd[1] + "=" + cmd[2]);
proc = rt.exec(cmd);
StreamGobbler e = new StreamGobbler(proc.getInputStream(), "ERROR");
StreamGobbler o = new StreamGobbler(proc.getInputStream(), "OUTPUT");
e.start();
o.start();
Date end = new Date();
println(sim.format(end) + ":服务启动完成........... ");
isRestart = false;
} catch (Exception e) {
println(e.getMessage());
}
}
}
static class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);