容器相关

区分单个容器多个实例的方法

1.Jboss domain
System.getProperties()参数中有[Server:server-one],可唯一确定实例

Set keys = System.getProperties().keySet();
if (null != keys) {
    for (Object key : keys) {
        String k = (String) key;
        if (k.startsWith("[Server:")) {
            return k.substring(k.lastIndexOf(":") + 1, k.lastIndexOf("]"));
        }
    }
}

2.Jboss standalone
jboss standalone模式可通过获取端口号来区分不同的实例。
获取端口号方式如下:
1)通过request请求获取端口号
2)容器刚启动时,没有request的场景下,方式如下:
2.1)JMX方式(未成功,待继续调查)

try {       
        MBeanServer server = null;
        if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
            server = MBeanServerFactory.findMBeanServer(null).get(0);
        }

        Set names = server.queryNames(new ObjectName("Catalina:type=Server,*"), null);

        Iterator iterator = names.iterator();
        ObjectName name = null;
        while (iterator.hasNext()) {
            name = (ObjectName) iterator.next();
            String port = null;
            String protocol = server.getAttribute(name, "protocol").toString();
            if(protocol.contains("org.apache.coyote.http11.Http11Protocol")) {
                port = server.getAttribute(name, "port").toString();
            }
            if(protocol.contains("org.apache.coyote.http11.Http11NioProtocol")) {
                port = server.getAttribute(name, "port").toString();
            }
            if(protocol.contains("org.apache.coyote.http11.Http11AprProtocol")) {
                port = server.getAttribute(name, "port").toString();
            }
            port = server.getAttribute(name, "port").toString();
                
            System.out.println(port);
        }
} catch (Exception e) {
    e.printStackTrace();
}

    2.2)读取配置文件方式(本例采取的方式)

private static String readPortFromXMLFile(String filePath, String tagName, String portName, String httpName) {
    String port = null;
    try {
        File xmlFile = new File(filePath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlFile);
            
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName(tagName);
            
        for(int i=0; i< nList.getLength(); i++) {
            Node nNode = nList.item(i);
            if(nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                if(null != httpName) {
                    if(eElement.getAttribute("name").equals(httpName)) {
                        port = eElement.getAttribute(portName);
                    }
                } else {
                    port = eElement.getAttribute(portName);
                }
            }
        }
    } catch (Exception e) {
        System.err.println("[FAIL] Error ocurres when reading port number from XMLFile");
        e.printStackTrace();
    }
    return port;
}
Properties prop = System.getProperties();
if(prop != null && prop.containsKey("jboss.server.base.dir")) {
     String instance = prop.getProperty("jboss.server.base.dir");
     String path = instance + File.separator + "configuration" + File.separator + "standalone.xml";
     String offsetStr = readPortFromXMLFile(path, "socket-binding-group", "port-offset", null);
     String offset = offsetStr.substring(offsetStr.lastIndexOf(":") + 1, offsetStr.length() - 1);
     String portStr = readPortFromXMLFile(path, "socket-binding", "port", "http");
     String port = portStr.substring(portStr.lastIndexOf(":") + 1, portStr.length() - 1);
     String server = String.valueOf(Integer.valueOf(port) + Integer.valueOf(offset));
     return server;
}

3.WAS
通过“server.root”和"sun.java.command"两个字段可区分不同实例。

Properties prop = System.getProperties();
if (prop != null && prop.containsKey("server.root")) {
    String server = prop.getProperty("server.root");
    String serverName = prop.getProperty("sun.java.command");
    serverName = serverName.substring(serverName.lastIndexOf(" ") + 1).trim();
    return server.substring(server.lastIndexOf("/") + 1)+"_"+serverName;
}

4.Tomcat
通过读取配置文件获取端口号的方式区分不同的实例。

Properties prop = System.getProperties();
if (prop != null && prop.containsKey("catalina.base")) {
    String instance = prop.getProperty("catalina.base");
    String path = instance + File.separator + "conf" + File.separator + "server.xml";
    String server = readPortFromXMLFile(path, "Server", "port", null);
    return server;
}           

5.Jetty
通过rjrport可区分不同的实例。

Properties prop = System.getProperties();
if (prop != null && prop.containsKey("rjrport")) {
    String server = prop.getProperty("rjrport");
    return server;
}

你可能感兴趣的:(容器相关)