代码之家  ›  专栏  ›  技术社区  ›  AlexR

确定应用程序正在应用程序服务器下运行

  •  0
  • AlexR  · 技术社区  · 14 年前

    有些代码可以在各种环境中重用,包括Java EE应用服务器。有时很高兴知道代码是否在application server下运行,以及它是哪个应用服务器。

    我更喜欢通过检查应用服务器的一些典型的系统属性来完成这项工作。 例如,它可能是

    • jboss.server.name 对于JBoss
    • catalina.base 为了Tomcat

    有人知道其他服务器的适当属性名吗? Weblogic、Websphere、Oracle IAS、其他?

    很容易检查是否安装了特定的应用程序服务器。只需添加行 对任何JSP、Servlet、EJB执行System.getProperties()并打印结果。

    我可以自己做,但安装服务器并使其工作需要很多时间。

    我读过这个讨论: How to determine type of Application Server an application is running on?

    但我更喜欢使用系统属性。这是一个简单且绝对可移植的解决方案。代码不依赖于任何其他API,如Servlet、EJBContext或JMX。

    3 回复  |  直到 8 年前
        1
  •  1
  •   Youssef NAIT Gor Sahakyan    7 年前

    这不是一种“标准”方法,但我所做的是尝试加载AppServer的一个类。

    因为是:

    try{  
    Class cl = Thread.getContextClassLoader().loadClass("com.ibm.websphere.runtime.ServerName");
    
    // found
    
    }  
    // not Found  
    catch(Throwable)
    {
    
    }
    
    // For Tomcat: "org.apache.catalina.xxx"
    

    等。

    告诉我你的想法

        2
  •  1
  •   Lukasz Stelmach    14 年前

    JBoss AS设置了许多不同的系统属性:

    jboss.home.dir
    jboss.server.name
    

    您可以使用VisualVM或其他工具检查其他属性。

    我不知道其他的服务器,但我想你可以为它们找到一些属性。

        3
  •  1
  •   Kazeem Akinbola    6 年前
    //for Tomcat
    try {
     MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
     ObjectName name = new ObjectName("Catalina", "type", "Server");
     StandardServer server = (StandardServer) mBeanServer.getAttribute(name,"managedResource");
     if (server != null) {
       //its a TOMCAT application server
     }
    } catch (Exception e) {
       //its not a TOMCAT Application server
    }
    
    //for wildfly
    try {
      ObjectName http = new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket- binding=http");
      String jbossHttpAddress = (String) mBeanServer.getAttribute(http, "boundAddress");
      int jbossHttpPort = (Integer) mBeanServer.getAttribute(http, "boundPort");
      String url = jbossHttpAddress + ":" + jbossHttpPort;
      if(jbossHttpAddress != null){
       //its a JBOSS/WILDFLY Application server
      }
    } catch (Exception e) {
       //its not a JBOSS/WILDFLY Application server
    }
    
    推荐文章