论坛首页 Java版

在运行期间获得WebLogic Server所监听的IP地址和端口号

浏览 1174 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-06-18 关键字: weblogic tips
每个WebLogic Server都可以配置其监听的IP地址和端口号,如下图:


在运行期间,如果应用需要知道WebLogic Server所监听的IP地址和端口号,可通过JMX编程获得,JSP的样例程序如下:
java 代码
 
  1. <%@ page import="javax.naming.Context, 
  2.                 javax.naming.InitialContext, 
  3.                 weblogic.management.MBeanHome, 
  4.                 weblogic.management.configuration.ServerMBean"   
  5. %>  
  6. <%!  
  7.     String  serverName, serverAddress;  
  8.     int     serverPort;  
  9.       
  10.     private void getServerInfo() throws Exception {  
  11.             Context myCtx = new InitialContext();  
  12.             MBeanHome home = (MBeanHome)myCtx.lookup(MBeanHome.LOCAL_JNDI_NAME);  
  13.             serverName=home.getMBeanServer().getServerName();  
  14.               
  15.             ServerMBean sc = (ServerMBean)home.getConfigurationMBean(serverName, "ServerConfig");  
  16.             serverAddress = sc.getListenAddress();  
  17.             serverPort = sc.getListenPort();  
  18.     }  
  19. %>  
  20.   
  21.   
  22.     
  23.   
  24. <%  
  25.     try {  
  26.         getServerInfo();  
  27.     } catch (Exception e) {  
  28.     }  
  29. %>  
  30.   

  31. Server Name: <%=serverName %> 
      
  32. Server Address: <%=serverAddress %> 
      
  33. Server Port: <%=serverPort %> 
      
  34.   
  35.    


将上述代码保存成jsp文件,放置到web app目录中,即可访问其得到服务器名字、IP地址和端口号。如果是匿名用户(尚未登陆)则会出现安全错误,因为WebLogic Server缺省情况下不允许匿名用户访问MBean信息,可在控制台中更改,如下图:


参考资料:
1)Programming WebLogic Management Services with JMX
2)Javadocs for WebLogic Classes

该JSP程序在WebLogic Server 8.1 SP6上测试通过。
  • 6e229ff3-994d-4eab-8c1c-4b8515949e14-thumb
  • 大小: 86.7 KB
  • Dfa24483-fd1b-4606-8137-38e286082ccb-thumb
  • 大小: 133.2 KB
   
论坛首页 Java版

跳转论坛:
JavaEye推荐