gpt4 book ai didi

Java MXBean 自定义类型

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:12:49 27 4
gpt4 key购买 nike

我正在尝试创建一个具有自定义属性的 MXBean,但我得到了 javax.management.NotCompliantMBeanExceptionIJmsDestinationMBean.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;
}
}

最佳答案

问题是接口(interface)IJmsDestinationMBean。它返回的类型 JmsDestinationAttributes 不是开放类型。这是我在执行此操作时遵循的经验法则:

  • 实际注册的 MBean(具有复杂类型属性)称为 Foo,它的管理接口(interface)称为 FooMXBean
  • 复杂类型(Foo的属性叫Bar,有一个管理接口(interface)叫BarMBean。这家伙不能返回任何非开放类型或其他适当公开的复杂类型的值。

因此(对于此示例)“宿主”MBean 需要是 MXBean 才能支持复杂类型,并且复杂类型需要有一个名为 MBean 的接口(interface)。注意一个是MXBean接口(interface),一个是MBean接口(interface)。

这是我的例子:

  • JMSDestination 实现 JMSDestinationMXBean
  • JmsDestinationAttributes 实现 JmsDestinationAttributesMBean

...为松散的案例标准道歉。这是一个即时示例。

这里是 JMSDestination 代码,带有要创建和注册的 ma​​in。我只是使用用户名属性来提供名称。:

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 View 如下所示:

JConsole view of the MXBean

MXBean 属性的 JConsole View 如下所示:

JConsole view of the MXBean's Attributes

有道理吗?

关于Java MXBean 自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14939335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com