标准MBean

Laying the MBean

MBean编码规则

1)MBean必须是具体的Java类(非抽象,可以实例化),agent使用反射来创建MBean。
2)MBean要么实现自己的MBean接口,要么是DynamicMBean接口。MBean接口可以是任何接口,遵循命名规则:ClassNameMBean。
3)MBean必须有public构造器
MBean是否实现javax.management.NotificationBroadcaster是可选的。此接口允许MBean发送通知给监听者。

使用标准MBean

    standard MBean属性是类成员变量,通过get,set方法暴露给管理者。standard MBean操作是public method。一旦创建standard MBean的管理接口不能改变。

组成标准管理接口

    MBean的所有信息都必须从管理接口处收集。管理接口包括一组通过MBean暴露的属性和操作,允许管理程序使用MBean,管理接口包括MBean的构造器和通知。

管理接口的组件

public constructor
    通过HTML adapter可以看到MBean是 可以被动态载入到JMX agent中。agents使用任何被MBean暴露的public构造器来实现。构造器包含在管理接口的定义中,因为特定的constructor基于MBean对象的生命周期定义了特定行为。例如一个构造器会告诉MBean记录所有的action,而其他的constructo不会。对于standard MBean,agent使用反省(introspection)来发现public constructor。  
Attributes
    attribute是MBean管理接口的至关重要部分。standard MBean通过声明getter和setter方法来暴露属性。
Operations
    operations与action对应的操作,在管理资源中初始化。standard MBean暴露的operation是除getter和setter的其他方法。
Notifications
    Notification允许MBean与注册的监听器通信。MBean想要发出通知,必须实现NotificationBroadcaster接口。

Standard MBean 继承模式

直接实现MBean接口

    MBean可以通过MBean接口的实现来创建。
继承管理接口
    有效的MBean可以通过继承其它有效standard MBean来实现。这种情况不是直接实现MBean接口,但它也是MBean。他继承了父类的管理接口。当想改变MBean的行为,但是保持了管理接口。

覆盖管理接口


    MBean只能实现一个简单的MBean接口。假如一个MBean继承其他的类实现了一个有效的MBean管理接口A,并实现了自己的MBean管理接口B,则JMX agent并不是识别管理接口A的方法。
扩展管理接口
    当一个MBean管理接口A继承另一个MBean管理接口B,则管理接口A暴露的方法是两个接口的方法。

扩展和覆盖的结合


     类似覆盖,JMX agent只是别最近的MBean管理接口。JMX agent识别CopierPrinterMBean接口,但由于CopierPrinterMBean扩展了PrinterMBean和DeviceMBean,所以CopierPrinter的MBean管理接口包含三个。

扩展非MBean接口

    

    当MBean接口扩展一个非MBean接口,结果暴露的属性和操作由MBean接口和它扩展的接口决定。

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMX(Java Management Extensions)提供了一种标准的方式来管理Java应用程序。MBean是JMX的核心概念之一,它是一种管理Bean的方式,允许运行时动态地管理和监控Java应用程序。MBean可以分为两种类型:开放类型和封闭类型。 开放类型的MBean是一种非常灵活的MBean,它允许在运行时动态地定义属性和操作,并且可以通过JMX远程访问这些属性和操作。开放类型的MBean通常用于管理和监控非常动态的Java应用程序,例如Web应用程序、消息系统、集群系统等。 开放类型的MBean可以通过实现javax.management.DynamicMBean接口来创建。DynamicMBean接口定义了一组方法,包括getMBeanInfo()、getAttribute()、setAttribute()、invoke()等,这些方法允许动态地定义和访问MBean的属性和操作。 下面是一个示例代码,演示如何创建一个开放类型的MBean: ```java import javax.management.*; public class MyDynamicMBean implements DynamicMBean { private String message; public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { if (attribute.equals("message")) { return message; } else { throw new AttributeNotFoundException(attribute); } } public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { if (attribute.getName().equals("message")) { message = (String) attribute.getValue(); } else { throw new AttributeNotFoundException(attribute.getName()); } } public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { if (actionName.equals("printMessage")) { System.out.println(message); return null; } else { throw new ReflectionException(new NoSuchMethodException(actionName), "Cannot find the operation " + actionName); } } public MBeanInfo getMBeanInfo() { MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[] { new MBeanAttributeInfo("message", "java.lang.String", "The message", true, true, false) }; MBeanOperationInfo[] operations = new MBeanOperationInfo[] { new MBeanOperationInfo("printMessage", "Print the message", null, "void", MBeanOperationInfo.ACTION) }; return new MBeanInfo(getClass().getName(), "My Dynamic MBean", attributes, null, operations, null); } } ``` 在上面的代码中,我们实现了一个MyDynamicMBean类,并实现了DynamicMBean接口中的方法。该MBean只有一个message属性和一个printMessage操作,它们都是动态定义的,可以在运行时动态地修改和访问。 要注册和访问该MBean,可以使用JMX API提供的方法,例如: ```java MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.example:type=MyDynamicMBean"); MyDynamicMBean mbean = new MyDynamicMBean(); mbs.registerMBean(mbean, name); ``` 以上代码将该MBean注册到JMX MBeanServer中,并指定一个ObjectName用于访问该MBean。其他的Java应用程序或工具可以使用JMX API来访问该MBean,并动态地修改和访问其属性和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值