hudeya 发表于 2017-2-16 09:53:28

使用JMX监控WebLogic 8.x的解决方案

在写WebLogic监控程序的过程中,可以说是相当坎坷,因为因为我要支持监控WebLogic不同版本,而WebLogic不同版本对JMX的支持却有着不小的区别,在网上也没有找到完整的解决方案,所以只能自己一点点的摸索,如今终于拨云见日了,OMG。在此,我们只探讨监控WebLogic 8.x的解决方案,其他版本后续跟进,我会整理出一个完整的文章。为什么监控WebLogic 8.x要先单独写个文章呢——因为8.x跟新的9.x和10.x不一样,刚开始入手会遇到很麻烦的问题。如果我入手之前能看到这篇文章,嘿嘿,那就谢天谢地了。

好了,不啰嗦了,我们开始:

首先第一条,要访问WebLogic Server 8.x的MBeans,必须是在JDK1.4环境下,否则会很麻烦(需要写自己classloader),原因如下:

Java代码
因为WebLogic 8.x的weblogic.jar中有自己的javax.management包,而JDK1.5中也有javax.management包(以前版本没有),运行时会优先调用JDK1.5中javax.management包里的类,因此产生错误。
而且weblogic8.x支持JMX 1.0,而JDK1.5支持JMX1.2 。
网上有人说通过System.setProperty("jmx.serial.form", "1.0");这样的设置可以解决问题,经试验根本不行。

使用JMX访问WebLogic Server 8.x的MBeans,需要使用的JAR包是weblogic.jar、webservices.jar
可以在%WL_HOME%\server\lib目录下找到。


如果只加入weblogic.jar包,有些API可以访问,但是运行MBeanHome->getAllMBeans()方法获取所有MBean时会抛异常:

Java代码
weblogic.rjvm.PeerGoneException: ; nested exception is:   
    weblogic.utils.NestedException: Ljavax/xml/soap/SOAPException; - with nested exception:

    at weblogic.rjvm.BasicOutboundRequest.sendReceive(BasicOutboundRequest.java:108)
    at weblogic.rmi.internal.BasicRemoteRef.invoke(BasicRemoteRef.java:164)
    at weblogic.management.internal.AdminMBeanHomeImpl_815_WLStub.getAllMBeans(Unknown Source)
    at cn.chenfeng.jmx.weblogic.ListAllMBeans.main(ListAllMBeans.java:27)
Caused by: weblogic.utils.NestedException: Ljavax/xml/soap/SOAPException; - with nested exception:

    at weblogic.rjvm.RJVMImpl.gotExceptionReceiving(RJVMImpl.java:967)
    at weblogic.rjvm.ConnectionManager.gotExceptionReceiving(ConnectionManager.java:930)
    at weblogic.rjvm.t3.T3JVMConnection.hasException(T3JVMConnection.java:870)
    at weblogic.socket.SocketMuxer.deliverExceptionAndCleanup(SocketMuxer.java:588)
    at weblogic.socket.SocketMuxer.deliverHasException(SocketMuxer.java:544)
    at weblogic.socket.JavaSocketMuxer.processSockets(JavaSocketMuxer.java:306)
    at weblogic.socket.SocketReaderRequest.execute(SocketReaderRequest.java:32)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)
--------------- nested within: ------------------
weblogic.rmi.extensions.RemoteRuntimeException: Unexpected Exception - with nested exception:
[weblogic.rjvm.PeerGoneException: ; nested exception is:   
    weblogic.utils.NestedException: Ljavax/xml/soap/SOAPException; - with nested exception:
]
    at weblogic.management.internal.AdminMBeanHomeImpl_815_WLStub.getAllMBeans(Unknown Source)
    at cn.chenfeng.jmx.weblogic.ListAllMBeans.main(ListAllMBeans.java:27)


加入webservices.jar包就可以解决问题了。

下面展示一下我自己跑的Demo


Java代码
package cn.chenfeng.jmx.weblogic;

import java.util.Iterator;
import java.util.Set;

import javax.naming.Context;

import weblogic.jndi.Environment;
import weblogic.management.Helper;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;
import weblogic.management.WebLogicObjectName;
import weblogic.management.runtime.RuntimeMBean;

public class JMXDemo1 {

    public static void main(String[] args) throws Exception {

      final String providerUrl = "t3://192.168.1.2:7001";
      final String username = "chenfeng";
      final String password = "chenfeng";

      MBeanHome adminHome = (MBeanHome) Helper.getAdminMBeanHome(username,
                password, providerUrl);
      System.out.println("DomainName : " + adminHome.getDomainName());

      /*
         * 获取MBeanHome的另一个方法,Helper内部就是通过这种方法实现的
         */
      {
            Environment env = new Environment();
            env.setProviderUrl(providerUrl);
            env.setSecurityPrincipal(username);
            env.setSecurityCredentials(password);
            Context ctx = env.getInitialContext();

            MBeanHome localHome2 = (MBeanHome) ctx
                  .lookup(MBeanHome.LOCAL_JNDI_NAME);
            MBeanHome adminHome2 = (MBeanHome) ctx
                  .lookup(MBeanHome.ADMIN_JNDI_NAME);
            System.out.println("DomainName : " + localHome2.getDomainName());
            System.out.println("DomainName : " + adminHome2.getDomainName());
      }

      {
            Set mbeans = adminHome.getMBeansByType("ServerRuntime");
            System.out.println("ServerRuntime MBeans : " + mbeans);
      }

      /*
         * JDBC连接池
         */
      {
            System.out
                  .println("\n===============获取JDBC连接池信息==================");
            Set mbeans = adminHome.getMBeansByType("JDBCConnectionPoolRuntime");
            System.out.println("JDBCConnectionPoolRuntime MBeans : " + mbeans);

            Iterator itt = mbeans.iterator();
            while (itt.hasNext()) {
                RuntimeMBean mbean = (RuntimeMBean) itt.next();
                String name = mbean.getName();
                int count = Integer.parseInt(mbean.getAttribute(
                        "ActiveConnectionsCurrentCount").toString());
                System.out.println("【JDBCConnectionPoolRuntime --> " + name
                        + " --> ActiveConnectionsCurrentCount】: " + count);
            }
      }

      /*
         * 线程池
         */
      {
            System.out.println("\n===============获取线程池信息==================");
            Set mbeans = adminHome.getMBeansByType("ThreadPoolRuntime");
            System.out.println("ThreadPoolRuntime MBeans : " + mbeans);

            Iterator itt = mbeans.iterator();
            while (itt.hasNext()) {
                RuntimeMBean mbean = (RuntimeMBean) itt.next();
                String name = mbean.getName();
                int count = Integer.parseInt(mbean.getAttribute(
                        "ExecuteThreadIdleCount").toString());
                System.out.println("【ThreadPoolRuntime --> " + name
                        + " --> ExecuteThreadIdleCount】: " + count);
            }
      }

      /*
         * 获取所有的MBean信息
         */
      {
            System.out
                  .println("\n===============打印从AdminHome获得的所有MBean==================");
            Set allMBeans = adminHome.getAllMBeans();
            System.out.println("MBean的总数目:" + allMBeans.size());
            for (Iterator itr = allMBeans.iterator(); itr.hasNext();) {
                WebLogicMBean mbean = (WebLogicMBean) itr.next();
                WebLogicObjectName objectName = mbean.getObjectName();
                System.out.println(objectName.getName() + " is a(n) "
                        + mbean.getType());
            }
      }

         
    }
}
页: [1]
查看完整版本: 使用JMX监控WebLogic 8.x的解决方案