StandardEmitterMBean only in JDK1.6

Since I am using JDK1.5, there doesn't seem to be a way to mimic StandardEmitterMBean.

Based on http://blogs.sun.com/jmxetc/entry/javax_management_standardmbean_when_and

I am currently doing the following:

/**

* where {@code IFoo} is my MBean interface

*/

Fooimplements IFooextends NotificationBroadcasterSupport

{

..

}

StandardFooextends StandardMBean

{

..

}

However my bean is not emitting events right now. So I'm suspecting that there are more changes in StandardEmitterMbean that I will need to implement.

I'm not sure if the code for StandardEmitterMbean is GPL.

Thanks

Gavin

[934 byte] By [chihiroa] at [2007-11-26 18:14:41]
# 1

I managed to get this to work.

public class StandardEmitterMBean extends StandardMBean implements NotificationEmitter, NotificationPropagater

{

public StandardEmitterMBean( Object impl, Class interface, NotificationEmitter emitter )

{

super( impl, interface );

if ( implementation instanceof IPlatformMBean )

( ( IPlatformMBean ) implementation ).setNotificationPropagater( this );

}

...

public propagateNotification( Notification notification )

{

sendNotification( notification );

}

}

interface NotificationPropagater

{

propagateNotification( Notification notification );

}

basically I maintain a reference to a subclass of NotificationBroadcasterSupport. And my actual MBean (the implementation above) will delegate the sending of the notification to its wrapper i.e. StandardEmitterMBean.

Does my original MBean need to extend from NotificationBroadcasterSupport ?

regards,

Gavin

chihiroa at 2007-7-9 5:48:10 > top of Java-index,Core,Monitoring & Management...
# 2

Hi,

On JDK 1.5 you will need to code your own subclass of StandardMBean that implements

NotificationEmitter.

This is shows how it can be done:

public class CustomStandardEmitterMBean

extends StandardMBean implements NotificationEmitter {

final private NotificationEmitter emitter;

<T> public CustomStandardEmitterMBean(T impl, Class<T> interfaceClass,

NotificationEmitter emitter) throws ... {

super(impl,interfaceClass);

if (emitter == null) throw new IllegalArgumentException("emitter");

this.emitter = emitter;

}

//-

// implements all methods of NotificationEmitter interface by delegating to

// emitter

public void addNotificationListener(...) {

emitter.addNotificationListener(....);

}

...

public void removeNotificationListener(...) {

emitter.removeNotificationListener(....);

}

...

public MBeanNotificationInfo[] getNotifications() {

return emitter.getNotifications();

}

// end of NotificationEmitter interface implementation

//-

// We need also to override getMBeanInfo() in order to add the

// MBeanNotificationInfo[] array the first time getMBeanInfo() is called

// or if the MBeanNotificationInfo[] array changed.

//

public MBeanInfo getMBeanInfo() {

// Let superclass evaluate MBeanInfo

final MBeanInfo info = super.getMBeanInfo();

// Check whether it has the correct notification infos

final MBeanNotificationInfo[] notifInfo = getNotifications();

if (!Arrays.deepEquals(info.getNotifications(),notifInfo)) {

// Doesn't have correct notif info => need to create a new MBeanInfo

final MBeanInfo newInfo = new MBeanInfo(

info.getClassName(), info.getDescription(),

info. getAttributes(), info.getOperations(),

notifInfo);

// Let the superclass replace its cached info by our new info

cacheMBeanInfo(newInfo);

return newInfo;

} else {

// Previously cached version is OK - just return it.

return info;

}

}

}

(Disclaimer: code mostly written from memory).

Then you would create your MBean as follows:

public class SomeMBeanImpl extends NotificationBroadcasterSupport

implements SomeMBean {

... this is your MBean implementation, which emits notification ...

}

// Create an instance of SomeMBeanImpl

final SomeMBeanImpl impl = new SomeMBeanImpl(...);

// Wrap it in CustomStandardEmitterMBean

final StandardMBean myStandardEmitter =

new CustomStandardEmitterMBean(impl ,SomeMBean.class,impl);

Maybe I'll add this to my blog... If I get the time ;-)...

Hope this helps,

-- daniel

JMX, SNMP, Java, etc...

http://blogs.sun.com/jmxetc

dfuchsa at 2007-7-9 5:48:10 > top of Java-index,Core,Monitoring & Management...
# 3
cool stuff Daniel. Conceptually I was correct. But my code has a lot casts, which I don't like.ThanksGavin
chihiroa at 2007-7-9 5:48:10 > top of Java-index,Core,Monitoring & Management...