質問

is there a way to get the list of bean instances that spring has registered with the mbeanserver?

I saw that you can register a MBeanExporterListener on the MBeanExporter, but that only tells me the ObjectName that a bean was registered with. Can I use that ObjectName somewhere to get the instance of the Object that was registered?

I see that one option could be to subclass MBeanExporter, but I really don't want to do that unless I have to.

Thanks.

役に立ちましたか?

解決

Turns out you can't. You have to subclass MBeanExporter if you want to achieve this.

他のヒント

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

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. ObjectInstance represent the object name of an MBean and its class name. 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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top