PDA

View Full Version : NotificationPublisher never gets set



ewgmail
Nov 8th, 2006, 05:23 PM
Hi,

I have a problem implementing a notification publisher in my bean. I implement NotificationPublisherAware but spring never sets my noficationPublisher. Can someone please enlighten on what I am doing wrong.

Thanks
Eric

package com.acme.app.management;

import javax.management.Notification;

import org.springframework.jmx.export.annotation.ManagedR esource;
import org.springframework.jmx.export.notification.Notifi cationPublisher;
import org.springframework.jmx.export.notification.Notifi cationPublisherAware;

import com.acme.eventframework.BaseEventListener;
import com.acme.eventframework.Event;
import com.acme.eventframework.EventFilter;

@ManagedResource(objectName = "bean:name=eventListener", description = "event listener", log = true, logFile = "jmx.log", currencyTimeLimit = 15)
public class NotificationEmitterEventListener extends BaseEventListener implements NotificationPublisherAware {

private NotificationPublisher notificationPublisher;
private long sequenceNumber = 0;

public NotificationEmitterEventListener() {
this.setEventFilter(new EventFilter() {
public boolean evaluate(Event event) {
return true;
}
});
}

public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
this.notificationPublisher = notificationPublisher;
}

public void handleEvent(Event event) {
// Map the event to the JMX event
Notification notification = new Notification(event.getTopic(), this, sequenceNumber, event.getMessage());
notification.setUserData(event.getData());
if (notificationPublisher != null) {
notificationPublisher.sendNotification(notificatio n);
}
}

}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!-- this bean must not be lazily initialized if the exporting is to happen -->
<bean id="exporter"
class="org.springframework.jmx.export.MBeanExporter">
<property name="assembler" ref="assembler" />
<property name="namingStrategy" ref="namingStrategy" />
<property name="beans">
<map>
<entry key="bean:name=eventListener" value-ref="notificationEmitterEventListenerListener"/>
</map>

</property>
</bean>

<bean id="jmxAttributeSource"
class="org.springframework.jmx.export.annotation.Annotati onJmxAttributeSource" />

<!-- will create management interface using annotation metadata -->
<bean id="assembler"
class="org.springframework.jmx.export.assembler.MetadataM BeanInfoAssembler">
<property name="attributeSource" ref="jmxAttributeSource" />
</bean>

<!-- will pick up ObjectName from annotation -->
<bean id="namingStrategy"
class="org.springframework.jmx.export.naming.MetadataNami ngStrategy">
<property name="attributeSource" ref="jmxAttributeSource" />
</bean>


<bean id="notificationEmitterEventListener"
class="acme.app.management.NotificationEmitterEventListen er">
<property name="eventDispatcher" ref="eventDispatcher" />
</bean>
</beans>

qbit
Dec 4th, 2006, 09:54 AM
It seems the setter is never invoked by spring. As a workaround i used the notification mechanism from javax.management......

Dave Syer
Dec 6th, 2006, 01:05 PM
I think you just need to explicitly set the notificationListenerMappings property on the MBeanExporter, e.g.



<property name="notificationListenerMappings">
<map>
<entry key="bean:name=beanIWantToEmitNotifications">
<ref bean="notificationEmitterEventListener">
</entry>
</map>
</property>


It looks from your example that you are trying to notify a bean of its own events (i.e. it's surprising that the listener is itself an MBean). Nothing illegal about that, but possibly not what you intended.

ewgmail
Dec 14th, 2006, 06:56 PM
I think you just need to explicitly set the notificationListenerMappings property on the MBeanExporter, e.g.



<property name="notificationListenerMappings">
<map>
<entry key="bean:name=beanIWantToEmitNotifications">
<ref bean="notificationEmitterEventListener">
</entry>
</map>
</property>


It looks from your example that you are trying to notify a bean of its own events (i.e. it's surprising that the listener is itself an MBean). Nothing illegal about that, but possibly not what you intended.


Thanks, I'll give that a try. I'm actually trying to convert events in our internal event bus, to jmx events for external monitoring.

toxonix
Feb 13th, 2007, 11:35 AM
I couldnt get this to work either. The container never sets the NotificationPublisher.
Also, I didn't find spring's notifcation wrappers particularly helpful, so I stuck to pure JMX. Its better not to include the dependency on more spring APIs if it is uncertain whether they will work the same between versions.