关闭

Java监控activeMQ(JMX)

315人阅读 评论(0) 收藏 举报
分类:
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<String, String[]> props = new HashMap<String, String[]>();
        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");
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15078次
    • 积分:335
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:5篇
    • 译文:1篇
    • 评论:0条