I want to monitor the JVM, but I don't know where to start!

table of Contents

1. What is jmx?

2. What are the official offers?

3. Implement your own standard mbean

4. Application scenarios

5. Summary


In development, it is often necessary to monitor the operation of the application, including information such as memory, cpu, gc, and thread. Many of these tools are provided by jdk. If we want to implement some of our own management, how can we obtain this information? Today, let's take a look at the starting point of all this-Jmx.

1. What is jmx?

JMX-Java Management Extensions, translated as Java management extensions, is a set of monitoring framework provided by jvm. Provides a unified interface for program monitoring, allowing you to monitor resource usage in the JVM without writing JNI.

2. What are the official offers?

mxbean official provides some commonly used, such as the following two pictures, basically can see the name can tell, which information can provide jvm.

 

What's the use of so much?

3. Implement your own standard mbean

How did so much of this happen? 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);
}

2. Implement the interface

/**
* 香菜聊游戏
*/
public class PlayerMgr implements PlayerMgrMBean {
  private String name ;
  @Override
  public String getName() {
      return name;
  }
  @Override
  public void setName(String newName) {
      this.name = newName;
  }
  @Override
  public void helloPlayer(String worldStr) {
      System.out.println(name + " " + worldStr);
  }
}

3. Register mbean

  /**
* 香菜聊游戏
*/
  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("注册完毕");
      Thread.sleep(24*60*60*1000);
  }

4. Observe the mbean.

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

According to your registered name, open your registered mbean, for example, the name registered in my code is "corg.pdool.jmx".

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

helloPlayer corresponds to the exposed interface method, p1 indicates that the parameter that can be passed in is String, which can be modified directly. If it is passed in "Hello", and then click the helloPlayer button, the background will output the printing information.

4. Application scenarios

The existence of technology must be an application scenario. What are some famous applications that use jmx? For example, arthas, the middleware software WebLogic management page is developed based on JMX, and JBoss is based on the JMX architecture.

5. Summary

The mbean bluntly means that the MBeanServer is started in the application. The user registers according to their own needs, and then connects to the client at runtime to obtain information. The standard CS structure is just different rules. Obey the interface rules and there is nothing special , Saves the time of customizing JNI, nothing more. These provided by the system can already achieve most of the monitoring requirements. If you want to customize, just remember the interface rules, and let the content play freely. Have you learnt that?

Writing braille is not easy, like it, pay attention, and forward three qualities. Thank you for your support.

One sentence per day

If you aim at the moon, you will fall between the stars even if you are lost.

Published 106 original articles · Like 381 · Visits 510,000+

Guess you like

Origin blog.csdn.net/perfect2011/article/details/105646663