1

Some code may be reused in various environments including Java EE Application server. Sometimes it is nice to know whether the code is running under application server and which application server is it.

I prefer to do it by checking some system property typical for the application server. For example it may be

  • jboss.server.name for JBoss
  • catalina.base for Tomcat

Does somebody know appropriate property name for other servers? Weblogic, Websphere, Oracle IAS, others?

It is very easy to check if you have the specific application server installed. Just add line System.getProperties() to any JSP, Servlet, EJB and print the result.

I can do it myself but it will take a lot of time to install server and make it working.

I have read this discussion: How to determine type of Application Server an application is running on?

But I prefer to use system property. It is easier and absolutely portable solution. The code does not depend on any other API like Servlet, EJBContext or JMX.

4

3 回答 3

2

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

jboss.home.dir
jboss.server.name

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

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

于 2010-11-21T11:52:45.783 回答
1

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

对于 WAS:

try{  
Class cl = Thread.getContextClassLoader().loadClass("com.ibm.websphere.runtime.ServerName");

// found

}  
// not Found  
catch(Throwable)
{

}

// For Tomcat: "org.apache.catalina.xxx"

等等。

让我知道你的想法

于 2010-11-21T02:48:23.697 回答
1
//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
}
于 2018-09-14T18:26:43.713 回答