JMX 实例

通过JMX的基础架构图,使用代码简单操作JMX。

struct.png

这里需要使用 HtmlAdaptorServer 这个类,需要增加一下依赖。

<dependency>
    <groupId>org.glassfish.external</groupId>
    <artifactId>opendmk_jdmkrt_jar</artifactId>
    <version>1.0-b01-ea</version>
</dependency>

HelloMBean.java

public interface HelloMBean {

  String getName();

  void setName(String name);

  void printHello();

  void printHello(String name);
}

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(name);
  }

  @Override
  public void printHello(String helloName) {
    System.out.println(helloName);
  }
}

HelloAgent.java

public class HelloAgent {

  public static void main(String[] args) throws Exception {

    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    String domain = "MyMBean";
    ObjectName objectName = new ObjectName(domain + ":name=HelloWorld");
    platformMBeanServer.registerMBean(new Hello(), objectName);

    // distribute layer html adaptor
    //HtmlAdaptor是一个简单的HttpServer,它将Http请求转换为JMX Agent的请求
    ObjectName adaptorName = new ObjectName(domain + ":name=htmladapter,port=8082");
    HtmlAdaptorServer htmlAdaptorServer = new HtmlAdaptorServer();
    htmlAdaptorServer.start();

    platformMBeanServer.registerMBean(htmlAdaptorServer, adaptorName);
    // rmi 端口
    int rmiPort = 1099;
    // 替代手动 rmi register 注册
    Registry registry = LocateRegistry.createRegistry(rmiPort);
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:"+rmiPort+"/"+domain);
    JMXConnectorServer jmxConnector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, platformMBeanServer);
    jmxConnector.start();

  }
}

浏览器访问 localhost:8082 可以进入 web 管理页面,可以对自定义的MBean进行操作。

20190213160958.png

另外,还可以通过代码的访问来操作MBean。

HelloClient.java

public class HelloClient {

  public static void main(String[] args) throws Exception{
     String domain = "MyMBean";
     // rmi 端口
     int rmiPort = 1099;

    JMXServiceURL serviceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:"+rmiPort+"/"+domain);
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
    MBeanServerConnection mBeanServerConnection = connector.getMBeanServerConnection();
    System.out.println("Domains:------------------");
    for (String domain2 : mBeanServerConnection.getDomains()) {
      System.out.println("domain : " + domain2);
    }
    // MBean Count
    System.out.println("MBean Count: " + mBeanServerConnection.getMBeanCount());
    // set attribute
    ObjectName objectName = new ObjectName(domain+":name=HelloWorld");
    mBeanServerConnection.setAttribute(objectName, new Attribute("Name", "from HelloClient"));
    System.out.println("Name = "+mBeanServerConnection.getAttribute(objectName, "Name"));
    // execute method
    // via proxy
    HelloMBean proxy = MBeanServerInvocationHandler.newProxyInstance(mBeanServerConnection, objectName, HelloMBean.class, false);
    proxy.printHello();
    proxy.printHello("hello zhangsan");
    // via rmi
    mBeanServerConnection.invoke(objectName, "printHello", null, null);
    mBeanServerConnection.invoke(objectName, "printHello", new Object[]{"hello lisi"}, new String[]{String.class.getName()});

    connector.close();
  }
}
HelloClient output:
Domains:------------------
domain : JMImplementation
domain : java.util.logging
domain : java.lang
domain : com.sun.management
domain : MyMBean
domain : java.nio
MBean Count: 24
Name = from HelloClient
HelloAgent output:
from HelloClient
hello zhangsan
from HelloClient
hello lisi

参考: https://blog.csdn.net/u013256816/article/details/52800742

仅有 1 条评论
  1. 6666666666666666666666

添加新评论