Java MXBean自定义类型

swo*_*ded 3 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 8

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

  • 实际注册的MBean(具有复杂的类型属性)称为Foo,它的管理接口称为FooMXBean.
  • 复杂类型(Foo的属性称为Bar,并且有一个名为BarMBean的管理接口.这个人不能返回任何非开放类型或其他正确暴露的复杂类型的值.

因此(对于此示例),"host"MBean需要是MXBean才能支持复杂类型,并且复杂类型需要具有名为<ClassName> MBean的接口.请注意,一个具有M X Bean接口,另一个具有MBean接口.

这是我的例子:

  • JMSDestination实现JMSDestinationMXBean
  • JmsDestinationAttributes实现了JmsDestinationAttributesMBean

......为宽松的案例标准道歉.这是一个动态的例子.

这里是JMSDestination代码,主要用于创建和注册.我只是使用用户名属性来提供名称:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

JMSDestinationMXBean代码:

public interface JmsDestinationMXBean {
    public JmsDestinationAttributes getAttributes();
}
Run Code Online (Sandbox Code Playgroud)

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

....和JmsDestinationAttributesMBean:

public interface JmsDestinationAttributesMBean {
    public String getName();
    public int getMessagesCurrentCount();
    public int getConsumersCurrentCount();
}
Run Code Online (Sandbox Code Playgroud)

JConsole视图如下所示:

MXBean的JConsole视图

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

MXBean的属性的JConsole视图

合理 ?