【问题标题】:Determine that application is running under application server确定应用程序正在应用程序服务器下运行
【发布时间】:2011-05-13 04:30:36
【问题描述】:

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

我更喜欢通过检查应用程序服务器的一些典型系统属性来做到这一点。 例如它可能是

  • jboss.server.name JBoss
  • catalina.base 用于 Tomcat

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

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

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

我已阅读此讨论:How to determine type of Application Server an application is running on?

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

【问题讨论】:

    标签: java jakarta-ee application-server


    【解决方案1】:

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

    jboss.home.dir
    jboss.server.name
    

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

    我不知道其他服务器,但我认为您可以为每个服务器找到某种属性。

    【讨论】:

      【解决方案2】:

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

      对于 WAS:

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

      等等

      让我知道你的想法

      【讨论】:

        【解决方案3】:
        //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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多