JMX ModelMBean

    Model Mbean是唯一的,因为开发者不需要写MBean类。组成Model MBean的类和接口由JMX规范定义,且在每个JMX agent中都可以获取。Model MBean是可以在MBean server中实例化的通用的MBean,可以被用户配置来管理任何资源。
    Model MBean通过javax.management.modelbmean.RequiredModelMBean来定义。实际上ReqiuiredModelMBean是下了ModelMBean接口,且ModelMBean继承了DynamicMBean接口。ModelMBean管理接口在MBean的外部定义,通过set方法插入MBean。Model MBean的创建步骤:
    1)application启动
    2)调用MBean server的createMBean方法,创建RequiredModelMBean实例。
    3)设置被管理的管理资源
    4)创建ModelMBeanInfoSupport类的实例,此对象像MBeanInfo一样,封装了新ModelMBean的管理接口。
    5)invoke ModelMBean上的操作。
    ModelMBean可以持久化,通过持久化,ModelMBean可以在JMX agent整个生命周期存活,创建ModelMBean是尅配置多长时间保存它的状态。ModelMBean可以记录每次通知的输出,在输出通知时,允许指定日志文件位置。
    ModelMbean可以缓存属性值。缓存多久被更新,可以被缓存策略指定。
private ModelMBean createRawModelMBean(){
      RequiredModelMBean modelmbean = null;
      try
      {
         final ModelMBeanInfoSupport modelMBeanInfo =
            new ModelMBeanInfoSupport(
               SimpleCalculator.class.getName(),
               "A simple integer calculator.",
               null,      // attributes
               null,      // constructors
               buildModelMBeanOperationInfo(),
               null,      // notifications
               buildDescriptor() );
         modelmbean = new RequiredModelMBean(modelMBeanInfo);
         setModelMBeanManagedResource(modelmbean);
      }
      catch (MBeanException mbeanEx)
      {
         System.err.println(  "ERROR trying to create a ModelMBean:\n"
                            + mbeanEx.getMessage() );
      }
      return modelmbean;
}
 modelMBean.setManagedResource(new SimpleCalculator(),"ObjectReference");


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMX(Java Management Extensions)是Java平台的一个管理和监控API,可以让开发人员开发Mbean(Managed Bean)来管理和监控Java应用程序。Mbean是一种Java对象,通过JMX可以公开其属性和方法,以便对其进行管理和监控。 下面是一个简单的示例,演示如何使用JMX开发一个Mbean: 1. 定义一个实现了DynamicMBean接口的类,该类表示一个Mbean: ```java public class SimpleMBean implements DynamicMBean { private String name; public SimpleMBean(String name) { this.name = name; } @Override public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { if (attribute.equals("Name")) { return name; } else { throw new AttributeNotFoundException(attribute); } } @Override public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { throw new AttributeNotFoundException(attribute.getName()); } @Override public AttributeList getAttributes(String[] attributes) { AttributeList result = new AttributeList(); for (String attr : attributes) { try { result.add(new Attribute(attr, getAttribute(attr))); } catch (Exception ex) { // ignore } } return result; } @Override public AttributeList setAttributes(AttributeList attributes) { return new AttributeList(); } @Override public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { throw new ReflectionException(new NoSuchMethodException(actionName)); } @Override public MBeanInfo getMBeanInfo() { MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[] { new MBeanAttributeInfo("Name", "java.lang.String", "name", true, false, false) }; MBeanConstructorInfo[] ctors = new MBeanConstructorInfo[] { new MBeanConstructorInfo("SimpleMBean()", "Default constructor", new MBeanParameterInfo[0]) }; return new MBeanInfo(getClass().getName(), "SimpleMBean", attrs, ctors, null, null); } } ``` 2. 创建一个MBeanServer对象,并将Mbean注册到MBeanServer中: ```java MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.example:type=SimpleMBean"); SimpleMBean mbean = new SimpleMBean("hello"); mbs.registerMBean(mbean, name); ``` 3. 使用JConsole或其他JMX客户端连接到MBeanServer,可以查看和修改Mbean的属性。 这是一个非常简单的示例,实际上,Mbean可以包含更复杂的逻辑和更多的属性。Mbean也可以使用注解来简化Mbean的开发和管理。JMX是一个强大的工具,可以帮助开发人员监控和管理Java应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值