Java MXBean 自定义类型

2024-02-05

我正在尝试创建具有自定义属性的 MXBean,但出现 javax.management.NotCompliantMBeanException IJmsDestinationMBean.getAttributes 具有无法转换为开放类型的参数或返回类型

我读到 MXBean 属性必须与 OpenType 兼容。 我如何让我的属性以这种方式工作? 下面的所有类都在同一个包中。

class JmsDestinationMBean implements IJmsDestinationMBean{

  protected JmsDestinationAttributes attributes = new JmsDestinationAttributes();

  @Override
  public JmsDestinationAttributes getAttributes() {
    return this.attributes;
  }
}

@MXBean
interface IJmsDestinationMBean {
  JmsDestinationAttributes getAttributes()
}

class JmsDestinationAttributes {

  protected String name
  protected int messagesCurrentCount
  protected int consumersCurrentCount

  String getName() {
    this.name;
  }

  int getMessagesCurrentCount() {
    this.messagesCurrentCount;
  }

  int getConsumersCurrentCount() {
    this.consumersCurrentCount;
  }
}

问题出在接口上IJmsDestinationMBean。它返回一个类型Jms 目的地属性这不是开放类型。以下是我执行此操作时遵循的经验法则:

  • 实际注册的 MBean(具有复杂类型属性)称为Foo它的管理接口称为FooMXBean.
  • 复杂类型(属性Foo叫做Bar并且有一个名为的管理界面BarMBean。这家伙cannot返回任何不是开放类型或其他正确公开的复杂类型的值。

因此(对于本示例)“主机”MBean 需要是 MXBean 才能支持复杂类型,并且复杂类型需要有一个名为MBean。注意其中一个有MX一个有Bean接口,另一个有MBean接口。

这是我的例子:

  • JMS目的地实施JMSDestinationMXBean
  • Jms 目的地属性实施JmsDestinationAttributesMBean

...对于宽松的案例标准表示歉意。这是一个动态的例子。

这里是 JMSDestination 代码,带有main创建并注册。我只是使用用户名属性来提供名称:

public class JmsDestination implements JmsDestinationMXBean {
    protected JmsDestinationAttributes attrs = new JmsDestinationAttributes(System.getProperty("user.name"));

    public JmsDestinationAttributes getAttributes() {
        return attrs;
    }

    public static void main(String[] args) {
        JmsDestination impl = new JmsDestination();
        try {
            ManagementFactory.getPlatformMBeanServer().registerMBean(impl, new ObjectName("org.jms.impl.test:name=" + impl.attrs.getName()));
            Thread.currentThread().join();
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }
}

JMSDestinationMXBean 代码:

public interface JmsDestinationMXBean {
    public JmsDestinationAttributes getAttributes();
}

JmsDestinationAttributes 代码使用相同的名称和随机数作为值:

public class JmsDestinationAttributes implements JmsDestinationAttributesMBean {
    protected final String name;
    protected final Random random = new Random(System.currentTimeMillis());
    public JmsDestinationAttributes(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public int getMessagesCurrentCount() {
        return Math.abs(random.nextInt(100));
    }

    public int getConsumersCurrentCount() {
        return Math.abs(random.nextInt(10));
    }
}

....以及 JmsDestinationAttributesMBean:

public interface JmsDestinationAttributesMBean {
    public String getName();
    public int getMessagesCurrentCount();
    public int getConsumersCurrentCount();
}

JConsole 视图如下所示:

MXBean 属性的 JConsole 视图如下所示:

合理 ?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java MXBean 自定义类型 的相关文章

随机推荐