Custom Java MXBeans

I am trying to create an MXBean with a custom attribute, but I am getting javax.management.NotCompliantMBeanException IJmsDestinationMBean.getAttributes has a parameter type or return type that cannot be cast to a public type

I read that the MXBean attributes must be OpenType compatible. How can I get my attribute to work this way? All the classes below are in one package.

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;
  }
}

      

+3


source to share


1 answer


The problem is with the IJmsDestinationMBean interface . It returns JmsDestinationAttributes , which is not an open type. Here are the rules:

  • The actual registered MBean (having a complex typed attribute) is called Foo , and the management interface is called FooMXBean .
  • Complex type (the Foo attribute is named Bar and has a BarMBean control interface . This guy cannot return any values ​​that are not public types or other correctly mapped complex types.

So (for this example) the MBean for the host must be an MXBean to support complex types, and the complex type must have an interface named <ClassName> MBean . Note that it has an M X Bean interface and the other has an MBean interface.

Here's my example:

  • JMSDestination implements JMSDestinationMXBean
  • JmsDestinationAttributes implements JmsDestinationAttributesMBean

... apologies for the free case standard. This is an example on the fly.

Here's the JMSDestination code with main to create and register. I am just using the username property to provide the name .:

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 code:

public interface JmsDestinationMXBean {
    public JmsDestinationAttributes getAttributes();
}

      



JmsDestinationAttributes code that uses the same name and random numbers for values:

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));
    }
}

      

.... and JmsDestinationAttributesMBean:

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

      

The JConsole looks like this:

JConsole view of the MXBean

The JConsole view of the MXBean attributes looks like this:

JConsole view of the MXBean's Attributes

Make sense?

+8


source







All Articles