JMX监听weblogic10

jmx监听weblogic10的文档:http://download.oracle.com/docs/cd/E12840_01/wls/docs103/wlsmbeanref/core/index.html

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;

import weblogic.health.HealthState;

public class WeblogicJmxTest {
    private static MBeanServerConnection connection;

    private static JMXConnector connector;

    private static final ObjectName service;

    /*
     * 实例化 DomainRuntimeServiceMBean 对象名,这样可以通过类使用此对象名.
     */
    static {
        try {
            service = new ObjectName(
                    "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
        } catch (MalformedObjectNameException e) {
            throw new AssertionError(e.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {
        String hostname = ip;
        String portString = port;
        String username = "weblogic";
        String password = "weblogic";
        WeblogicJmxTest demo = new WeblogicJmxTest();
        demo.initConnection(hostname, portString, username, password);
        demo.printNameAndState(demo.getServerRuntimes());
        connector.close();
    }

    /*
     * 实例化与 Domain Runtime MBean Server 的连接。
     */
    private 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<String, String> h = new Hashtable<String, String>();
        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();
    }

    /*
     * 打印一组 ServerRuntimeMBeans.此 MBean 是运行时 MBean 层次的根,此域中的每个服务器承载自己的实例.
     */
    public ObjectName[] getServerRuntimes() throws Exception {
        return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
    }

    /*
     * 迭代 ServerRuntimeMBean,获取名称和状态
     */
    public void printNameAndState(ObjectName[] p_objNames) throws Exception {
        ObjectName[] serverRT = p_objNames;
        int length = (int) serverRT.length;
        for (int i = 0; i < length; i++) {
            print("=============Weblogic运行信息===============", "");
            // 域名称
            String name = (String) connection.getAttribute(serverRT[i], "Name");
            System.out.println("域名称: " + name);
            // 运行状态
            String state = (String) connection.getAttribute(serverRT[i], "State");
            System.out.println("运行状态: " + state);
            
            System.out.println("监听端口: " +  connection.getAttribute(serverRT[i], "ListenPort"));
            
            {
                ObjectName ob1 = (ObjectName) connection.getAttribute(serverRT[i], "ThreadPoolRuntime");
                ObjectName obj1_ob2 = (ObjectName) connection.getAttribute(ob1, "Parent");
                if(obj1_ob2!=null){
                    System.out.println(connection.getAttribute(obj1_ob2, "Type"));
                    int threadPool_health = ((HealthState) connection.getAttribute(obj1_ob2, "HealthState")).getState() ;  
                    print("ob2_HealthState", HealthState.mapToString(threadPool_health));
                }
            }
        }
    }

    public void print(String prefix, String content) {
        System.out.println(prefix + ": " + content);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值