The class javax.management.StandardMBean allows you to create a custom Standard MBean. This is useful when (a) your MBean can't have the naming link between the class Something and the interface SomethingMBean, and (b) when you want to override certain methods that allow you to customize the contents of the MBeanInfo for your MBean.
However, if your MBean must be a NotificationBroadcaster (or NotificationEmitter) then you must do the following:
(1) Subclass StandardMBean.
(2) Override getMBeanInfo() so that it calls super.getMBeanInfo() and the constructs another MBeanInfo that takes the attributes, operations, etc and adds the MBeanNotificationInfo[] appropriate for your MBean.
(3) Implements the NotificationEmitter interface, typically by forwarding its four methods to an instance of NotificationBroadcasterSupport.
This is all doable, but tedious. It would be very useful if a subclass of StandardMBean were already defined to do this for you. It might look something like this:
public class StandardEmitterMBean extends StandardMBean implements NotificationEmitter {
public <T> StandardEmitterMBean(T implementation, Class<T> mbeanInterface,
NotificationEmitter emitter) {...}
protected StandardEmitterMBean(Class<?> mbeanInterface,
NotificationEmitter emitter) {...}
// ...methods from NotificationEmitter implemented by forwarding to
// this.emitter
}
Now, to make a StandardMBean that emits notifications, you do this:
- construct your MBeanNotificationInfo[]
- construct a NotificationBroadcasterSupport whose getMBeanInfo() returns
your MBeanNotificationInfo[] (see RFE 4506105 for an easy way to do this)
- construct a StandardEmitterMBean with the appropriate implementation class
and your NotificationBroadcasterSupport
There is an even easier option, which is for the "implementation" parameter to already implement NotificationEmitter (for instance by subclassing NotificationBroadcasterSupport), in which case you can do:
new StandardEmitterMBean(impl, SomethingMBean.class, impl);
Although it is tempting to avoid adding a new class and simply add additional constructors to the existing StandardMBean class, this is not a good idea. For it to work, StandardMBean would have to implement NotificationEmitter. Much existing code assumes that if a class implements NotificationEmitter then it really does emit notifications, but with this change *all* StandardMBeans would implement NotificationEmitter.
###@###.### 2005-04-08 12:59:58 GMT