Essentials of JMX API - MXBeans

An MXBean is a new type of MBean that provides a simple way to code an MBean that only references a pre-defined set of types. In this way, you can be sure your MBean will be usable by any client, including remote clients, without any requirement that the client have access to model-specific classes representing the type of your MBean. MXBean provides a convenient way to bundle related values together without requiring clients to be specialty configured to handle the bundle.

In the same way as for standard MBeans, an MXBean is defined by writing a java interface called SomethingMXBean and a java class that implements that interface. However, unlike standard MBeans, MXBeans do not require java class to be called Something. Every method in the interface defines either an attribute or an operation in the MXBean. The annotation @MXBean can be also used to annotate the java  interface instead of requiring the interface's name to be followed by the MXBean suffix.

MXBeans provide a convenient way to bundle related values together in an MBean without requiring clients to be specially configured to handle the bundles when interacting with that MBean.

The MXBean contains the following files:

  • QueueSamplerMXBean interface
  • QueueSampler class that implements the MXBean interface
  • QueueSample Java type returned by getQueueSample method in the MXBean interface
  • Main the program that sets up and runs the example

The MXBean example performs the following action:

  • Define a simple MXBean that manages the a source of type  Queue
  • Declares a getter, getQueueSample, in the MXBean that takes a snapshot of the queue when invoked and returned a java class QueueSample that bundles the follows the following values together.
    • 1. The time the snapshot was token.
    • 2. The queue size.
    • 3. The head of the queue at that given time.
  • Registers the MXBean in an MBean server.

public interface QueueSamplerMXBean {
    public QueueSample getQueueSample();
    public void clearQueue();
}

As you can see, you declare an MXBean interface in exactly the same way as you declare a standard MBean. The QueueSampleMXBean interface declares two operations, getQueueSample and clearQueue.

public class QueueSampler implements QueueSamplerMXBean {

    private Queue queue;

    public QueueSampler(Queue queue) {
       this.queue = queue;
    }

    public QueueSample getQueueSample() {
        synchronized (queue) {
            return new QueueSample(new Date(), queue.size(), queue.peek());
        }
    }

    public void clearQueue() {
        synchronized (queue) {
            queue.clear();
        }
    }
}

The getQueueSample operation simply returns an instance of the QueueSample Java type, created with values returned by the java.util.Queeu methods peek and size and an instance of Date.

import java.beans.ConstructorProperties;
import java.util.Date;

public class QueueSample {

    private final Date date;
    private final int size;
    private final String head;

    @ConstructorProperties({"date", "size", "head"})
    public QueueSample(Date date, int size, String head) {
        this.date = date;
        this.size = size;
        this.head = head;
    }

    public Date getDate() {
        return date;
    }

    public int getSize() {
        return size;
    }

    public String getHead() {
        return head;
    }
}

 

In this class, the MXBean framework calls all the getters in QueueSample to convert the given instance into a CompositeData and uses the @ConstructorProperties annotation to reconstruct a QueueSample instance from a CompositeData.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章