您的位置:首页 > 运维架构 > Tomcat

java控制tomcat的启动和停止(包含linux模式)

2014-06-09 17:26 399 查看
tomcat目录结构:

tomcat

   --bin

           --EPG

                 --EPG-201403120000.jar

           --startup.sh  

在windows下将tomcat注册为服务(注册为服务的方式可以百度下),启动和停止采用"net start/stop 服务名"的方式。

对于linux或者aix,当前类的路径在上图中的jar包中,可以通过相对路径找到startup.sh进行启动;停止时,用shutdown.sh有时候不能真正的停止进程,需要找到对应的进程号,然后用程序杀掉。代码如下:

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

public class TomcatController {

 public static void startTomcatService() {

  if (isOsWindows()) {

   excuteRutimeCommand("net start " + Configure.tomcatServiceName);

  } else {

   startEPSInLinux();

  }

 }

 public static void stopTomcatService() {

  if (isOsWindows()) {

   excuteRutimeCommand("net stop " + Configure.tomcatServiceName);

  } else {

   stopEPSInLinux();

  }

 }

 private static List excuteRutimeCommand(String command) {

  System.out.println(command);

  List exeResultList = new ArrayList();

  try {

   Process p = Runtime.getRuntime().exec(command);

   BufferedReader br = new BufferedReader(new InputStreamReader(

     p.getInputStream()));

   String line = null;

   while ((line = br.readLine()) != null) {

    System.out.println(line);

    exeResultList.add(line);

   }

   br.close();

  } catch (Exception e) {

   e.printStackTrace();

  }

  return exeResultList;

 }

 private static boolean isOsWindows() {

  Properties prop = System.getProperties();

  String os = prop.getProperty("os.name");

  System.out.println(os);

  if (os.startsWith("win") || os.startsWith("Win")) {

   return true;

  }

  return false;

 }

 private static void startEPSInLinux() {

  List exeResultList = excuteRutimeCommand(new String[] { "/bin/sh",

    "-c", "ps -ef|grep EPS/bin/bootstrap.jar|awk '{print $2}'" });

  int epsProcessNum = 0;

  for (int i = 0; i < exeResultList.size(); i++) {

   String line = (String) exeResultList.get(i);

   if (null == line || line.trim().equalsIgnoreCase("")) {

    continue;

   }

   try {

    Long.parseLong(line.trim());

    epsProcessNum++;

   } catch (Exception e) {

   }

  }

  if (epsProcessNum >= 2) {

   return;

  }

  excuteRutimeCommand("../startup.sh");

 }

 private static void stopEPSInLinux() {

  List exeResultList = excuteRutimeCommand(new String[] { "/bin/sh",

    "-c", "ps -ef|grep EPS/bin/bootstrap.jar|awk '{print $2}'" });

  for (int i = 0; i < exeResultList.size(); i++) {

   String line = (String) exeResultList.get(i);

   if (null == line || line.trim().equalsIgnoreCase("")) {

    continue;

   }

   try {

    Long.parseLong(line);

    excuteRutimeCommand("kill -9 " + line);

   } catch (Exception e) {

   }

  }

 }

 private static List excuteRutimeCommand(String[] command) {

  System.out.println(command[2]);

  List exeResultList = new ArrayList();

  try {

   Process p = Runtime.getRuntime().exec(command);

   BufferedReader br = new BufferedReader(new InputStreamReader(

     p.getInputStream()));

   String line = null;

   while ((line = br.readLine()) != null) {

    System.out.println(line);

    exeResultList.add(line);

   }

   br.close();

  } catch (Exception e) {

   System.out.println(e.getMessage());

   e.printStackTrace();

  }

  return exeResultList;

 }

 public static void main(String[] args) throws Exception {

  Configure.load();

  startTomcatService();

  stopTomcatService();

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: