当项目要运行在多种应用服务器上,由于不同的服务器有某些不同的特性,就需要根据不同的应用服务器做不同的处理
Liferay里面提供了一个方法来判断不同的应用服务器,类ServerDetector就是用来判断当前应用是在哪个服务器下面的
使用很简单
- if (ServerDetector.isTomcat()) {
- }else if (ServerDetector.isWebLogic()) {
- }
但是如果我们想在运行时获取相应的端口、服务器信息就不太容易了(Tomcat可以直接读取server.xml文件,Weblogic也可以读取config.xml,但是在集群时里面的标签不好判断了)
http://www.iteye.com/problems/45571
就是上面这位兄弟的问题,在不发生request的情况下获取相应的端口信息,供集群环境下的调度器使用,在网上找了半天没有找到答案,最终在一个外国网站上找到了解答,方法如下:
- ObjectName service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
- InitialContext ctx = new InitialContext();
- MBeanServer server = (MBeanServer)ctx.lookup("java:comp/env/jmx/runtime");
- ObjectName rt = (ObjectName)server.getAttribute(service,"ServerRuntime");
- System.out.println("Server Name : "+server.getAttribute(rt,"Name"));
- System.out.println("Server Address : "+server.getAttribute(rt,"ListenAddress"));
- System.out.println("Server Port : "+server.getAttribute(rt,"ListenPort"));
- ctx.close();