JMX快速认知

扫码查看

一、介绍

JMX名词解释Java Management Extensions,即Java管理扩展。

能做什么

管理应用、查看设备信息、查看应用资源、监听资源变化

调用方面

支持跨平台、多协议(soap、http、html、snmp、jconsole)

二、历史变迁

14 Dec, 19983JMX 1.1和1.2制定JMX规范,为Web、分布式、动态性和模块化方案等,提供管理体系架构撤销在Java 5.2版本成为了Oracle公司商标Sun MicrosystemsConsumer & EmbeddedEmbedded System Software Group
30 May,200070JMX 适配IIOP协议规范建立基于IIOP协议的适配器,以支持CORBA客户端访问JMX代理撤销企业战略调整,不看好JMX在CORBA客户端的应用IONA Technologies PlcBen Beazley
27 Jun, 200071JMX-TMN定义规范电信管理网络(TMN)和JMX之间的互操作性撤销应规范负责人要求撤回,JSR3中已提及这块支持,只是一直没实现Bull S.A. (BullSoft)
21 Aug, 2001146JMX 支持WBem服务定义对于WBEM 服务提供协议适配器撤销JSR48 WBEN规范服务,由WBEN Solutions, Inc.提出。两家公司在提出此规范,一年半以后,合作停止Sun Microsystems, Inc.
27 Nov, 2001160JMX 远程调用API该API扩展了JMX1.2API,提供对JMX MBean服务器的远程访问。撤销与jsr3相同原因Sun Microsystems,Inc.
21 Sep, 2004255JMX 2.0更新JMX和JMX远程 API。改善现有功能可用性并添加新功能。撤销企业战略调整Sun Microsystems,Inc.
07 Dec, 2004262JMX 支持Web Services连接定义JMX支持Web Services支持非Java客户端。撤销企业战略调整Sun Microsystems,Inc.

可以看出JMX技术完全由Sun公司主导,被收购后没有新的规范被提出。实际被启用的只有sun公司所提交的3、160、262。

三、代码示例

具体地管理应用、查看设备信息、查看应用资源、监听资源变化,这些怎么实现的呢?

定义接口

//接口名必须以MBean结尾
public interface HelloMBean {
    String getName();

    void setName(String name);

    void printHello();
}

实现类

//类名必须和接口MBean前的内容相同,即类名+MBean必须等于接口名
public class Hello extends NotificationBroadcasterSupport implements HelloMBean {
    private AtomicLong sequenceNumber = new AtomicLong(1);
    private String name;

    @Override
    public void printHello() {
        System.out.println("你好," + name);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        String oldName = this.name;
        this.name = name;
        //通知方法
        Notification notification = new AttributeChangeNotification(this,
                sequenceNumber.getAndIncrement(),
                System.currentTimeMillis(),
                AttributeChangeNotification.ATTRIBUTE_CHANGE,
                "Name Change",
                "java.lang.String",
                oldName + "",
                this.name + "");
        super.sendNotification(notification);
    }
}

Main方法

public class StandardMBeanMain {
    public static void main(String[] args) throws Exception {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objName = new ObjectName("MBeanTest2:type=StandardMBean");
        mBeanServer.registerMBean(new Hello(), objName);
        mBeanServer.addNotificationListener(objName, new HelloListener(), null, null);
        Thread.sleep(60 * 60 * 1000);
    }
}

监听类

public class HelloListener implements NotificationListener {
    @Override
    public void handleNotification(Notification notification, Object handback) {
        log("SequenceNumber:" + notification.getSequenceNumber());
        log("Type:" + notification.getType());
        log("Message:" + notification.getMessage());
        log("Source:" + notification.getSource());
        log("TimeStamp:" + notification.getTimeStamp());
        log("UserData:" + notification.getUserData());
        log("oldValue:" + ((AttributeChangeNotification)notification).getOldValue());
        log("newValue" + ((AttributeChangeNotification)notification).getNewValue());
        log("attributeName:" + ((AttributeChangeNotification)notification).getAttributeName());
        log("=========" + notification.toString());
    }

    private void log(String message) {
        System.out.println(message);
    }
}

jconsole连接示例

ObjectName("MBeanTest2:type=StandardMBean")

查看bean属性信息,同时通过set方法支持修改bean属性

操作支持调用void方法

监听Notification 声明内容

引用

https://www.jcp.org/en/jsr/detail?id=3
https://www.jcp.org/en/jsr/detail?id=70
https://www.jcp.org/en/jsr/detail?id=71
https://www.jcp.org/en/jsr/detail?id=146
https://www.jcp.org/en/jsr/detail?id=160
https://www.jcp.org/en/jsr/detail?id=255
https://www.jcp.org/en/jsr/detail?id=262

12-24 09:07
查看更多