本文介绍了Java MXBean自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试使用自定义属性创建MXBean,但是我得到了javax.management.NotCompliantMBeanException IJmsDestinationMBean.getAttributes具有无法转换为开放类型的参数或返回类型 我已经读过MXBean属性必须与OpenType兼容。 如何让我的属性以这种方式工作? 以下所有类都在同一个包中。 class JmsDestinationMBean实现IJmsDestinationMBean { protected JmsDestinationAttributes attributes = new JmsDestinationAttributes(); @Override public JmsDestinationAttributes getAttributes(){ return this.attributes; } } @MXBean 接口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 。它返回一个类型 JmsDestinationAttributes ,它不是一个开放类型。以下是我在执行此操作时遵循的经验法则: 实际注册的MBean(具有复杂的类型属性)称为 Foo ,它的管理界面名为 FooMXBean 。 复杂类型( Foo 的属性被调用 Bar 并且有一个名为 BarMBean 的管理界面。这个 无法 返回任何非开放类型的值或其他正确暴露复杂类型。 因此(对于此示例),hostMBean需要是MXBean才能支持复杂类型,复杂类型需要有一个名为< ClassName> MBean 的接口。注意,一个具有M X Bean接口,另一个具有MBean接口。 / p> 这是我的例子: JMSDestination 实现 JMSDestinationMXBean JmsDestinationAttributes 实现 JmsDestinationAttributesMBean ...为宽松的案例标准道歉。这是一个动态的例子。 这里是JMSDestination代码,用 main 来创建和注册。我只是使用用户名属性来提供名称。: 公共类JmsDestination实现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代码,它使用相同的名称和值的随机数: 公共类JmsDestinationAttributes实现JmsDestinationAttributesMBean { protected final String name; protected final随机随机= 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视图如下所示: 有意义吗? I am trying to create an MXBean with a custom attribute, but I get javax.management.NotCompliantMBeanExceptionIJmsDestinationMBean.getAttributes has parameter or return type that cannot be translated into an open typeI have read that MXBean attributes have to be OpenType compatible.How would I make my attribute work this way?All the classes below are in the same package.class JmsDestinationMBean implements IJmsDestinationMBean{ protected JmsDestinationAttributes attributes = new JmsDestinationAttributes(); @Override public JmsDestinationAttributes getAttributes() { return this.attributes; }}@MXBeaninterface 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; }} 解决方案 The problem is the interface IJmsDestinationMBean. It returns a type JmsDestinationAttributes which is not an open type. Here's the rules-of-thumb I follow when doing this:The actual registered MBean (which has a complex typed attribute) is called Foo and it's management interface is called FooMXBean.The complex type (the attribute of Foo is called Bar and has a management interface called BarMBean. This guy cannot return any values that are not open types or other properly exposed complex types.So (for this example) the "host" MBean needs to be an MXBean in order to support complex types , and the complex type needs to have an interface called <ClassName>MBean. Note that one has the MXBean interface, and the other has the MBean interface.Here's my example:JMSDestination implements JMSDestinationMXBeanJmsDestinationAttributes implements JmsDestinationAttributesMBean...apologies for the loose case standard. It's an on the fly example.Here the JMSDestination code, with a main to create and register. I am simply using the user name 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); } }}The JMSDestinationMXBean code:public interface JmsDestinationMXBean { public JmsDestinationAttributes getAttributes();}The JmsDestinationAttributes code which uses the same name and random numbers for the 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 the JmsDestinationAttributesMBean:public interface JmsDestinationAttributesMBean { public String getName(); public int getMessagesCurrentCount(); public int getConsumersCurrentCount();}The JConsole view looks like this:The JConsole view of the MXBean's attributes looks like this:Make sense ? 这篇关于Java MXBean自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS! 05-23 15:41