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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值