JMX study notes (b)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

MBean is an ordinary Java object, the attributes, operations and notifications composition, except that it must comply with JMX specification defined pattern.

Specification defines an MBean of a class and an interface composition, and the interface name by a class name and a suffix MBean composition, such as Somthing SomthingMBean, methods defined in the interface is the MBean attributes and operations, compliance getter and methods setter specification for the MBean properties the method of operation in addition to the MBean.

MBean implementations illustrated

Interface definition

package com.example; 

public interface HelloMBean { 

	public void sayHello(); 
	public int add(int x, int y); 

	public String getName(); 
 
	public int getCacheSize(); 
	public void setCacheSize(int size); 
} 
  • HelloMbean method defined there: sayHello add
  • HelloMbean defined attributes: Name CacheSize. Name is read-only attribute; CacheSize to read or write.

Class definition

package com.example; 

public class Hello implements HelloMBean { 
    public void sayHello() { 
        System.out.println("hello, world"); 
    } 
     
    public int add(int x, int y) { 
        return x + y; 
    } 
     
    public String getName() { 
        return this.name; 
    }  
     
    public int getCacheSize() { 
        return this.cacheSize; 
    } 
     
    public synchronized void setCacheSize(int size) {
        ...
    
        this.cacheSize = size; 
        System.out.println("Cache size now " + this.cacheSize); 
    } 
    ...
     
    private final String name = "Reginald"; 
    private int cacheSize = DEFAULT_CACHE_SIZE; 
    private static final int 
        DEFAULT_CACHE_SIZE = 200; 
}

Registration MBean

The main task is to develop JMX reality MBean, then registered to the MBean Server. Therefore, we must first get the job MBean Server. MBean Server can get through ManagementFactory.getPlatformMBeanServer () method.

package com.example; 

import java.lang.management.*; 
import javax.management.*; 

public class Main { 
 
    public static void main(String[] args) 
        throws Exception { 
     
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
        ObjectName name = new ObjectName("com.example:type=Hello"); 
        Hello mbean = new Hello(); 
        mbs.registerMBean(mbean, name); 
          
        ...
     
        System.out.println("Waiting forever..."); 
        Thread.sleep(Long.MAX_VALUE); 
    } 
} 
  • Obtained by calling getPlatformMBeanServer () method, if it already exists MBean Server, then directly back ready; otherwise, create one. It means a single embodiment mode.
  • Each MBean instance by a name that uniquely identifies ObjectName, ObjectName JMX specification to follow, i.e. a domain name must be made and a set of Key-Value composition.

verification

  1. The program we write packing, and then execute the command window, as follows:
    Perform jar
  2. Open jmc.exe (installation directory bin JAVA) and then connected to the program, the browser can be seen MBean attributes and operations
    jmc.exe
    jmc.exe
  3. Perform sayHello operation, printed out in the command window "hello, world"
    sayHello result

Knowledge Chain

  1. JMX study notes (a)

Remark

  • This paper program fragment excerpt from Java Platform Standard Edition 8 Documentation document

reference

  • Java Platform Standard Edition 8 Documentation

Guess you like

Origin blog.csdn.net/qq_34017326/article/details/93642903
Recommended