United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6252526 : Create subclass of javax.management.StandardMBean that is a NotificationEmitter

Details
Type:
Enhancement
Submit Date:
2005-04-08
Status:
Resolved
Updated Date:
2010-07-29
Project Name:
JDK
Resolved Date:
2005-06-08
Component:
core-svc
OS:
generic,windows_xp
Sub-Component:
javax.management
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0,6
Fixed Versions:

Related Reports
Relates:
Relates:

Sub Tasks

Description
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

                                    

Comments
EVALUATION

Not much more needed than what is covered in the Description.
###@###.### 2005-04-08 12:59:58 GMT

Could be useful also to have shorthand constructors that take an MBeanNotificationInfo[] instead of NotificationEmitter and that make a NotificationEmitter based on that.  In that case, there would need to be a public sendNotification method that forwards to the private NotificationEmitter's sendNotification method.
###@###.### 2005-04-25 09:19:08 GMT
                                     
2005-04-08



Hardware and Software, Engineered to Work Together