• java程序监控tomcat中部署的项目的状态以及控制某些项目的启动停止


    原文:http://blog.csdn.net/liuyuqin1991/article/details/49280777

    步骤如下: 
    ①:首先授权用户使获得这些权限 
    You can find the role names in the web.xml file of the Manager web application. The available roles are:

    manager-gui — Access to the HTML interface. 
    manager-status — Access to the “Server Status” page only. 
    manager-script — Access to the tools-friendly plain text interface that is described in this document, and to the “Server Status” page. 
    manager-jmx — Access to JMX proxy interface and to the “Server Status” page.

    具体方法:修改/conf目录下的tomcat-users.xml文件,添加如下代码:

    <role rolename="manager-status"/>
    <role rolename="manager"/>  
    <role rolename="manager-jmx"/> 
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <role rolename="admin"/>
    <user username="admin" password="123456" roles="manager,manager-gui,admin,manager-status,manager-jmx,manager-script"/>

    这里创建一个admin 密码为123456的用户

    ②:创建通用tomcatUtil方法:

    public class TomcatHTMLUtil {
    
        public static String message(String operateURL) {
    
            StringBuffer dataResult = new StringBuffer();
            URL url = null;
            try {
                url = new URL(operateURL);
    
                URLConnection conn = (URLConnection) url.openConnection();
                //这里我是把admin,123456 这个用户信息放到了一个json文件中以json形式存放,然后取出来,如果不是以这种方式存放,则可以直接设置username = admin ,password =123456
                String data = FileUtil.readJson();
    
                String username = data.substring(1,data.length()-1).split(",")[0].split(":")[1].substring
                        (1,data.substring(1,data.length()-1).split(",")[0].split(":")[1].length()-1);
                String password =data.substring(1,data.length()-1).split(",")[1].split(":")[1].substring
                        (1,data.substring(1,data.length()-1).split(",")[0].split(":")[1].length());
    
                String configuration = username+":"+password; // manager角色的用户
                String encodedPassword = new BASE64Encoder().encode(configuration.getBytes());
                conn.setRequestProperty("Authorization", "Basic " + encodedPassword);
                // URL授权访问 -- End
    
                InputStream is = conn.getInputStream();
                BufferedReader bufreader = new BufferedReader(new InputStreamReader(is));
                String line = null;
                while ((line = bufreader.readLine()) != null) {
                    dataResult.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return dataResult.toString();
        }
    }

    ③:

    /**
         * 获取tomcat正在运行的webApp的参数
         * @return
         */
        public ArrayList<WebApp> getTomcatWebAppData(){
    
            ArrayList<WebApp> webAppArrayList = new ArrayList<WebApp>();
    
            String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/list");
    
            String[] oldDataStrs = data.split("/");
    
            if(oldDataStrs[0].startsWith("OK")){
                for (int i = 0; i < oldDataStrs.length; i++) {
                    String name = oldDataStrs[i].split(":")[0];
                    if(name.startsWith("legacy-proxy")){
                        WebApp webApp = new WebApp();
                        webApp.setName(name);
                        if(oldDataStrs[i].split(":")[1].equals("running")){
                            if(oldDataStrs[i].split(":")[2].equals("0")){
                                webApp.setStatus("运行");
                            }
                            else{
                                webApp.setStatus("异常");
                            }
                        }
                        else if(oldDataStrs[i].split(":")[1].equals("stopped")){
                            if(oldDataStrs[i].split(":")[2].equals("0")){
                                webApp.setStatus("停止");
                            }
                            else{
                                webApp.setStatus("异常");
                            }
                        }
                        else{
                            webApp.setStatus("异常");
                        }
                        webAppArrayList.add(webApp);
                    }
                }
            }
            return webAppArrayList;
        }
    
    /**
         * 重新部署一个项目
         * @param webAppName
         * @return
         */
        public boolean reloadWebApp(String webAppName){
            String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/reload?path=/"+webAppName);
            if(data.startsWith("OK")){
                return true;
            }
            else {
                return false;
            }
        }
    
        /**
         * 停止一个项目
         * @param webAppName
         * @return
         */
        public boolean stopWebApp(String webAppName){
            String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/stop?path=/"+webAppName);
            if(data.startsWith("OK")){
                return true;
            }
            else {
                return false;
            }
        }
    
        /**
         * 开始一个项目
         * @param webAppName
         * @return
         */
        public boolean startWebApp(String webAppName){
            String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/start?path=/"+webAppName);
            if(data.startsWith("OK")){
                return true;
            }
            else {
                return false;
            }
        }

    webApp的domain对象:

    package domain;
    
    /**
     * Created by Administrator on 2015/10/20.
     * webApp的相关信息
     */
    
    public class WebApp {
    
        /**
         * 项目名称
         */
        private String name;
    
        /**
         * 运行状态 "成功" or "停止"
         */
        private String status;
    
    
    
    
    
    
        public String getName() {
            return name;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }

    基本上核心功能已经出来了,剩下的就是写桌面swing程序了,我就不贴代码了 
    OK,完成收工,其实是很简单的。

  • 相关阅读:
    vsftpd的详细配置讲解
    ActiveMQ安装与配置
    Apache安装完之后再安装其他模块
    搭建 LAMP apache2.4 + php5.5 + mysql5.5/6 配置文件没有作用的问题
    LAMP apache2.4 + php5.5 + mysql5.5/6
    进制之间的转换
    Mac 常用属性
    关于颜色值透明度的设置
    关于TextView 的属性
    Android 关于软键盘
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6110298.html
Copyright © 2020-2023  润新知