专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > JBoss

JBOSS 定期停止服务,启动服务脚本解决办法

发布时间:2010-06-14 17:12:30 文章来源:www.iduyao.cn 采编人员:星星草
JBOSS 定期停止服务,启动服务脚本
项目使用 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);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
RestartjbossService.println(type + "> " + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}

}
//写日志
static void println(String fileContent) {
String fileName = "restartJBoss"+ new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())) + ".log";
File path = new File(filePath);
if (!path.exists()) {
path.mkdir();
}
File file = new File(path, fileName);
// 如果文件长度大于10M,则改名成备份文件进行存储。
if (file.length() > 10 * 1024 * 1024) {
String rename = fileName+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: