简体   繁体   English

我如何获得Spring在jmx中注册的bean?

[英]How do I get the beans that spring has registered with jmx?

is there a way to get the list of bean instances that spring has registered with the mbeanserver? 有没有一种方法可以获取Spring已在mbeanserver中注册的bean实例列表?

I saw that you can register a MBeanExporterListener on the MBeanExporter, but that only tells me the ObjectName that a bean was registered with. 我看到您可以在MBeanExporter上注册MBeanExporterListener,但这仅告诉我注册了bean的ObjectName。 Can I use that ObjectName somewhere to get the instance of the Object that was registered? 我可以在某个地方使用该ObjectName来获取已注册对象的实例吗?

I see that one option could be to subclass MBeanExporter, but I really don't want to do that unless I have to. 我看到一个选择可能是子类化MBeanExporter,但是我真的不想这样做,除非必须这样做。

Thanks. 谢谢。

Turns out you can't. 原来你做不到。 You have to subclass MBeanExporter if you want to achieve this. 如果要实现此目标,则必须子类化MBeanExporter。

I had tested a sample code as follows 我已经测试了以下示例代码

    // Get the Platform MBean Server
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    // Construct the ObjectName for the MBean we will register
    ObjectName name = new ObjectName("com.example.mbeans:type=Hello");
    // Create the Hello World MBean
    Hello mbean = new Hello();
    // Register the Hello World MBean
    mbs.registerMBean(mbean, name);

    Set<ObjectInstance> instances = mbs.queryMBeans(name, null);
    ObjectInstance instance = (ObjectInstance) instances.toArray()[0];
    System.out.println("Class Name:t" + instance.getClassName());
    System.out.println("Object Name:t" + instance.getObjectName());

    // Wait forever
    System.out.println("Waiting forever...");
    Thread.sleep(Long.MAX_VALUE);

Of course there is interface HelloMBean and 当然有interface HelloMBean

class Hello extends NotificationBroadcasterSupport implements HelloMBean

The output is 输出是

Class Name:tcom.example.mbeans.Hello
Object Name:tcom.example.mbeans:type=Hello
Waiting forever...

Hope this helps! 希望这可以帮助!

Update : 更新:

public class ObjectInstance extends Object implements Serializable . public class ObjectInstance extends Object implements Serializable ObjectInstance represent the object name of an MBean and its class name. ObjectInstance表示MBean的对象名称及其类名称。 We cannot retrieve the reference to the object itself. 我们无法检索对对象本身的引用。

I guess the only way to operate on the registered mbeans is to use JMS/RMI connector(or HTML adaptors) to get connection of mbaean server, create mbaen proxy using JMX.newMBeanProxy corresponding to registered mbean and invoke methods on that. 我猜对已注册的mbean进行操作的唯一方法是使用JMS / RMI连接器(或HTML适配器)来获得mbaean服务器的连接,使用与已注册的mbean对应的JMX.newMBeanProxy创建mbaen代理并在其上调用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM