1 Mart 2023 Çarşamba

JMX Nedir

Giriş
3 tane önemli kavram var
1. MBeans
2. MBean Server (also known as a JMX Agent)
3. JMX Client
Şeklen şöyle

JMX is GUI ile kullanmak için JConsole veya Visual VM kullanılır

MBeans
M kelimesi Managed Bean anlamına gelir. Açıklaması şöyle
The MBean is what you read data from or write data to, thereby altering its state.
Açıklaması şöyle
JMX exposes management “beans” (MBeans), these are objects that represent control points in the application. Your application can publish its own beans which lets you expose functionality for runtime monitoring and configuration. This is very cool as you can export information that an administrator can wire directly to a dashboard (APM, Prometheus, Grafana, etc.) and use that for decision making.
...
Server applications run without a UI or with deep UI separation. JMX can often work as a form of user interface or even as a command line interface 
MBeans Geliştirme
Açıklaması şöyle
MBeans must first specify an Interface which defines the methods you wish to expose. The interface name should end with “MBean” so normally the interface is named “somethingMBean”. In this case we are using “DogMBean” as our name.
Now we need a concrete class that implements the “somethingMBean” interface and overrides its empty methods with code that actually does stuff. The concrete class is named after the “something” from “somethingMBean”.
In this case the concrete class is named “Dog” because our interface is named “DogMBean”.
Örnek
Şöyle yaparız
package com.myexample.jmx;
public interface DogMBean {    
  public int getAge();    
  public void setAge(int newAge);    
  public String getName();    
  public void setName(String newName);    
  public void bark();
}

package com.myexample.jmx;
public class Dog implements DogMBean {    
  private int age = 10; // Set a default age
  private String name = "Fido"; // Set a default name
  // Getter and setters  
  ...
  
  @Override
  public void bark() {
    System.out.println("Woof");
  }
}
Şöyle yaparız
import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import javax.management.ObjectName; public class SimpleMBeanServer { public static void main(String[] args) throws Exception { // Create a new MBean Server MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); // Create an "ObjectName" which contains the package name and name of the MBean ObjectName objectName = new ObjectName("com.myexample.jmx:type=Dog"); // Create an instance of our Dog MBean Dog dogMBean = new Dog(); // Register our Dog MBean with our MBean Server mbs.registerMBean(dogMBean, objectName); System.out.println("Custom MBean Server starting up..."); System.out.println("Waiting forever..."); Thread.sleep(Long.MAX_VALUE); // Keeps our MBean Server process alive } }
Açıklaması şöyle
The ManagementFactory class for our purposes basically builds PlatformMBeanServer objects by using the method getPlatformMBeanServer. While there are many more methods available for this class this is the only one we really care about for this blog post.

The ObjectName class expects a single String parameter. The string is broken into two sections separated by a colon. The first section is the “domain”, this is normally the package name by convention but can technically be any String. The second section is the “key” section. This section contains a series of comma separated key/value pairs in the format: “key=value”

The MBeanServer class contains a registerMBean method. This is what actually connects our MBean to the server so that it can be queried and manipulated by the outside world. If an MBean is not registered to the MBean Server then the server is unaware of its existence and will not expose that MBean to the outside world.

MBean Server
Açıklaması şöyle
The MBeans are then registered to an MBean Server which exposes the MBean objects to the outside world for reading or manipulation.
JMX Client
Açıklaması şöyle
And finally the JMX Client queries the MBean Server for data relating to the MBeans, the state of the MBeans can also be changed via requests to the MBean Server from the JMX Client.







Hiç yorum yok:

Yorum Gönder