Java监控activeMQ(JMX)

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;


public class JmxDemo {
    public static void main(String[] args) throws MalformedObjectNameException, InstanceNotFoundException,
            AttributeNotFoundException, MBeanException, ReflectionException, IOException {
        test();
    }


    public static void test() throws IOException, MalformedObjectNameException, InstanceNotFoundException,
            MBeanException, ReflectionException, AttributeNotFoundException {
        String user = "admin";
        String pw = "activemq";
        String ip = "192.168.1.230";
        String port = "11099";


        String[] credentials = new String[] { user, pw };
        Map props = new HashMap();
        props.put("jmx.remote.credentials", credentials);
        JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + ip + ":" + port + "/jmxrmi");
        JMXConnector connector = JMXConnectorFactory.connect(address, props);


        MBeanServerConnection mbsc = connector.getMBeanServerConnection();
        connector.connect();


        // 声明类 参数为ClassName 可以通过jconsole查看Mbean的属性
        ObjectName health = new ObjectName("org.apache.activemq:type=Broker,brokerName=broker1,service=Health");
        // 调用方法
        List list = (List) mbsc.invoke(health, "healthList", null, null);
        // 获取属性
        String status = (String) mbsc.getAttribute(health, "CurrentStatus");
    }
}

你可能感兴趣的:(代码)