Java MXBean 自定义类型

标签 java jmx mbeans

我正在尝试创建一个具有自定义属性的 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;
  }
}

最佳答案

问题是接口(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/

相关文章:

java - Quartz 运行一次作业

tomcat - 哪些 JMX 属性测量请求的数量及其处理时间?

java - 通过 jstatd 查看 MBean

jmx - 属性和项目的 Prometheus jmx 导出器模式匹配

Java 在小程序重新加载时杀死守护线程

java - URLrewrite 过滤器中的过滤器链

java - 将数据存储到 Parse 中时出现问题 - 使用 TextView

jakarta-ee - JMX 和 RMI 的区别

JMeter:JMX/MBeans 采样器?

java - 插入 MBean 拦截器