首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

WebLogic 中獲取 Server 資訊(IP 與 Port)的 MBean 實作

     編輯:Java 相關知識

應用場景

在 J2EE 開發中,常需要動態獲取當前 WebLogic Server 的監聽地址與埠號,例如進行 EJB 遠程調用、呼叫本機 Web Service 組件等。特別是當 Server 設定了 ListenAddress 時,localhost 可能無法訪問,此時必須透過 JMX MBean 取得真實的 IP 與 Port。

核心代碼實作

ObjectName rs = null;
MBeanServer mbeanServer = (MBeanServer) context.lookup("java:comp/env/jmx/runtime");
String RUNTIME_SERVICE_MBEAN = "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean";
rs = new ObjectName(RUNTIME_SERVICE_MBEAN);
ObjectName serverrt = (ObjectName) mbeanServer.getAttribute(rs, "ServerRuntime");

String listenAddr = (String) mbeanServer.getAttribute(serverrt, "ListenAddress");
String[] tempAddr = listenAddr.split("/");
if (tempAddr.length == 1) {
    listenAddr = tempAddr[0];
} else if (tempAddr[tempAddr.length - 1].trim().length() != 0) {
    listenAddr = tempAddr[tempAddr.length - 1];
} else if (tempAddr.length > 2) {
    listenAddr = tempAddr[tempAddr.length - 2];
}
String listenPort = String.valueOf(mbeanServer.getAttribute(serverrt, "ListenPort"));

關鍵點解析

  1. MBeanServer 獲取:透過 JNDI 查找 java:comp/env/jmx/runtime 獲得運行時 MBeanServer。
  2. RuntimeServiceMBean:WebLogic 運行時服務的入口,可進一步獲取 ServerRuntime。
  3. ListenAddress 處理:取得的值可能為 t3://ip:port 格式,需分割取最後有效段作為 IP。
  4. ListenPort 直接轉字串:屬性值為數值型,轉為 String 即可使用。

擴展應用

透過 ServerRuntime 還可獲取更多 Server 狀態資訊(如健康狀態、啟動時間、部署應用列表等),詳細屬性請參考官方文檔:

注意事項

代碼需運行於 WebLogic 容器內部(如 Servlet、EJB 中),且應用需具備 JMX 權限。若在叢集環境下,建議結合 ClusterRuntime 獲取所有節點資訊。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved