directory

What is JMX?

2. What is officially provided?

Implement your own standard MBeans

4. Application Scenarios

5, summary


In the development often need to monitor the running of the application, including memory, CPU, GC, thread and other information, these things the JDK provides a lot of tools, if we want to achieve some of their own management, how to obtain these information? Today, let’s take a look at where it all started — Jmx.

What is JMX?

JMX – Java Management Extensions, which translates to Java Management Extensions, is a set of monitoring frameworks provided by the JVM. Provides a unified interface for application monitoring, allowing you to monitor resource usage within the JVM without writing JNI.

2. What is officially provided?

Mxbeans officially provide some common ones, such as the following two figures, which basically tell you by name what information is available to the JVM.

 

What’s the use of all this?

Implement your own standard MBeans

How did all of this come about? Show me the fuck code.

1. Define the MBean interface

Public interface PlayerMgrMBean {public void setName(String newName); public String getName(); public void helloPlayer(String worldStr); }Copy the code

2. Implement interfaces

/** ** * game */
public class PlayerMgr implements PlayerMgrMBean {
  private String name ;
  @Override
  public String getName(a) {
      return name;
  }
  @Override
  public void setName(String newName) {
      this.name = newName;
  }
  @Override
  public void helloPlayer(String worldStr) {
      System.out.println(name + ""+ worldStr); }}Copy the code

3. Register an

Public static void main(String[] args) throws Exception {MBeanServer MBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName( "corg.pdool.jmx:type=playerMgr" ); mBeanServer.registerMBean(new PlayerMgr(), name); System.out.println(" registration completed "); Thread.sleep(24*60*60*1000); }Copy the code

4. Observe mBeans.

Open C: Program Files\Java\jdk1.8.0_121\bin\jconsole.exe, select the app you are running, then double-click to open it, connect, and read the data.

Open your registered mBean based on your registered name, such as “corg.pdool. JMX” in my code.

Attribute corresponds to the GET /set method. The attribute and signature information of the method are shown below.

HelloPlayer corresponds to the exposed interface method, p1 means that the parameter can be passed in is String, can be directly modified, if passed in “hello”, then click the helloPlayer button, background will output printed information.

4. Application Scenarios

The existence of technology is bound to have applications, what are the well-known applications that use JMX? Arthas, for example, middleware software WebLogic’s management page is based on JMX, and JBoss’s entire system is based on JMX.

5, summary

Mbeans basically start MBeanServer in the application, users register according to their own needs, and then make client connection at runtime to get information, standard CS structure, just different rules, follow the interface rules, nothing special, save time to customize JNI, that’s all. The system already provides most of the monitoring requirements, if you want to customize, just remember the interface rules, as for the content is free to play. Did you learn?

Writing braille is not easy, point a like, pay attention to, forward quality three even. Thank you for your support.

Daily sentence

If you aim for the moon, even if you get lost, you will fall among the stars.