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<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");
    }
}
阅读更多
个人分类: 代码
上一篇两台linux机器时间同步
下一篇Mysql 启动异常
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭