java · 2021-06-17 0

JMX的使用

一、概述

JMX(Java Management Extensions)是一个为应用程序植入管理功能的框架。JMX是一套标准的代理和服务。

主要用于对JAVA应用程序和JVM进行监控和管理。

JConsole能够监控到JAVA应用程序和JVM的相关信息都是通过JMX实现的。

二、JMX的使用

1.定义MBean接口

这个接口的名称要以“MBean”结束

HelloMBean.java

public interface HelloMBean {

    String getName();

    void setName(String name);

    void printHello();

    void printHello(String str);
}

2.实现具体的MBean

Hello.java

public class Hello implements HelloMBean {

    private String name;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void printHello() {
        System.out.println("hello...");
    }

    @Override
    public void printHello(String str) {
        System.out.println("hello..." + str);
    }
}

3.定义MBean接口

JackMBean.java

public interface JackMBean {

    void hi();
}

4.实现具体的MBean

实现这个MBean接口,并且继承NotificationBroadcasterSupport,来提供广播服务

Jack.java

public class Jack extends NotificationBroadcasterSupport implements JackMBean {

    private int seq = 0;

    @Override
    public void hi() {
        Notification notify = new Notification(
                "jack, hi", 
                this, 
                ++seq, 
                System.currentTimeMillis(), 
                "jack");
        sendNotification(notify);
    }
}

5.启动类

public class HelloAgent {

    public static void main(String[] args) throws Exception {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        Hello hello = new Hello();
        ObjectName helloName = new ObjectName("jmxBean:name=Hello");
        server.registerMBean(hello, helloName);

        Jack jack = new Jack();
        ObjectName jackName = new ObjectName("jack:name=Jack");
        server.registerMBean(jack, jackName);

        HelloListener helloListener = new HelloListener();
        jack.addNotificationListener(helloListener, null, hello);

        Thread.sleep(2000000);
    }
}

6.启动jconsole

打开%JAVA_HOME%/bin/jconsole连接到本地进程

jconsole

7.server

jmx开启服务供远程连接

HelloServer.java

public class HelloServer {

    public static void main(String[] args) throws Exception {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        Hello hello = new Hello();
        ObjectName helloName = new ObjectName("jmxBean:name=Hello");
        server.registerMBean(hello, helloName);

        Jack jack = new Jack();
        ObjectName jackName = new ObjectName("jack:name=Jack");
        server.registerMBean(jack, jackName);

        HelloListener helloListener = new HelloListener();
        jack.addNotificationListener(helloListener, null, hello);

        // 注册一个端口,绑定url后用于客户端通过rmi方式连接JXMConnectorServer
        LocateRegistry.createRegistry(8999);
        // URL路径的结尾可以随意指定,但如果需要Jconsole来进行连接,则必须使用jmx:rmi
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8999/jmxrmi");

        JMXConnectorServer jcs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);

        System.out.println("begin rmi start");

        jcs.start();

        System.out.println("rmi start");
        Thread.sleep(2000000);
    }
}

8.client

使用客户端连接jmx

JMXClient.java

public class JMXClient {

    public static void main(String[] args) throws Exception {
        String jmxUrl = "service:jmx:rmi:///jndi/rmi://localhost:8999/jmxrmi";

        JMXServiceURL jmxServiceURL = new JMXServiceURL(jmxUrl);

        JMXConnector jmxc = JMXConnectorFactory.connect(jmxServiceURL, null);

        MBeanServerConnection msc = jmxc.getMBeanServerConnection();

        String[] domains = msc.getDomains();

        for (String domain : domains) {
            System.out.println(domain);
        }

    }
}