jmx实现监控weblogic中的jms队列信息(一)

 

需要加入jar包:weblogic.jar,wlclient.jar,wljmsclient.jar,wls-api.jar,这些jar包在weblogic安装目录下可以找到(bea\weblogic92\server\lib,这是9.2版本),

 

下面是简单的代码,通过java代码基本能监控queue中的数据情况

 

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;

import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

public class MonitorJMS {
    private static MBeanServerConnection connection;
    private static JMXConnector connector;
    private static final ObjectName service;
    // Initializing the object name for DomainRuntimeServiceMBean
    // so it can be used throughout the class.
    static {
       try {
          service = new ObjectName(
           "com.bea:Name=DomainRuntimeService," +
           "Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
       }catch (MalformedObjectNameException e) {
          throw new AssertionError(e.getMessage());
       }
    }
    /*
    * Initialize connection to the Domain Runtime MBean Server
    */
    public static void initConnection(String hostname, String portString, 
       String username, String password) throws IOException,
       MalformedURLException { 
       String protocol = "t3";
       Integer portInteger = Integer.valueOf(portString);
       int port = portInteger.intValue();
       String jndiroot = "/jndi/";
       String mserver = "weblogic.management.mbeanservers.domainruntime";
       JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
          port, jndiroot + mserver);
       Hashtable h = new Hashtable();
       h.put(Context.SECURITY_PRINCIPAL, username);
       h.put(Context.SECURITY_CREDENTIALS, password);
       h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
          "weblogic.management.remote");
       connector = JMXConnectorFactory.connect(serviceURL, h);
       connection = connector.getMBeanServerConnection();
    }
    /*
    * Get an array of ServerRuntimeMBeans
    */
    public static ObjectName[] getServerRuntimes() throws Exception {
       return (ObjectName[]) connection.getAttribute(service,
          "ServerRuntimes");
    }
    
    public void getJmsQueueInfo() throws Exception {
        ObjectName[] serverRT = getServerRuntimes();
        ObjectName JMSRT = (ObjectName) connection.getAttribute(serverRT[0],
                                                                "JMSRuntime");
        ObjectName[] JMSServers = (ObjectName[]) connection.getAttribute(JMSRT,
                                                                         "JMSServers");
        int JMSServer_Length = (int) JMSServers.length;
        for (int x = 0; x < JMSServer_Length; x++) {
            //jmsserver名称
            String JMSServer_name = (String) connection.getAttribute(JMSServers[x],
                                                                     "Name");
            ObjectName[] JMSDests = (ObjectName[]) connection.getAttribute(JMSServers[x],
                                                                           "Destinations");
            int JMSdest_Length = (int) JMSDests.length;
            for (int y = 0; y < JMSdest_Length; y++) {
                //queue名称
                String queue_name = (String) connection.getAttribute(JMSDests[y],
                                                                     "Name");
                long pendingmcount = (Long) connection.getAttribute(JMSDests[y],
                                                                    "MessagesPendingCount");
                //当前队列中有多少条记录
                long currentcount = (Long) connection.getAttribute(JMSDests[y],
                                                                   "MessagesCurrentCount");

                System.out.println(y
                        + "--[jms server name]: "
                        + JMSServer_name
                        + "  [jms name]: "
                        + queue_name
                        + "   [pending]: "
                        + pendingmcount
                        + "   [current]: "
                        + currentcount);
                        
            }
        } 
    }
    
    public static void main(String[] args) throws Exception {
       String hostname = "127.0.0.1";
       String portString = "7001";
       String username = "weblogic";
       String password = "weblogic";
       MonitorJMS s = new MonitorJMS();
       initConnection(hostname, portString, username, password);
       s.getJmsQueueInfo();
       connector.close();
    }
}
 

更多weblogic MBean信息可以查看http://docs.oracle.com/cd/E13222_01/wls/docs92/wlsmbeanref/core/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值