Have we considered adding support for application event
filters, to allow registered listeners to filter events of
interest? I think that would be a nice feature, and would
be simplify listener handing code, as well as improve
performance.
So for example, we could define the following interface:
public interface ApplicationEventFilter {
public boolean isEventOfInterest(ApplicationEvent e);
}
public class LifecycleApplicationEventFilter {
public boolean isEventOfInterest(ApplicationEvent e) {
return (e instanceof LifecycleApplicationEvent);
}
}
public interface ApplicationEventMulticaster {
public void addApplicationListener(ApplicationListener
listener, ApplicationEventFilter filter);
}
public class SimpleApplicationEventMulticaster {
public void addApplicationListener
(ApplicationListener listener, ApplicationEventFilter
filter) {
this.applicationEventSubscriptions.add(new
ApplicationEventSubscription(listener, filter));
}
public void multicastEvent(ApplicationEvent event) {
Iterator it =
this.applicationEventSubscriptions.iterator();
while (it.hasNext()) {
ApplicationEventSubscription subscription =
(ApplicationEventSubscription) it.next();
if (subscription.isEventOfInterest(event)) {
subscription.broadcast(event);
}
}
}
}
This is analagous to JMX's
NotificationListener/NotificationFilter constructs
|