I have seen plenty of post saying that the buit-in event framework
is not ideal, is not robust enough... I have found it to be everything
that I need. Here is what I have used:
I have created one class to delegate events by the type
of class the event is, this helper has a map of ApplicationListener(s).
Then in a MyAbstractView that extends AbstractView I initialize an instance of the above class and call it in the onApplicationEvent method.Code:public class ApplicationEventHandler implements ApplicationListener { private HashMap applicationListeners = new HashMap(2); public void addApplicationListener(Class klass, ApplicationListener applicationListener) { applicationListeners.put(klass, applicationListener); } public void addAsynchronousApplicationListener(Class klass, ApplicationListener applicationListener) { applicationListeners.put(klass, new AsynchronousApplicationListener(applicationListener)); } /** * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent) */ public synchronized void onApplicationEvent(ApplicationEvent event) { ApplicationListener applicationListener = (ApplicationListener) applicationListeners.get(event.getClass()); if (applicationListener != null) { applicationListener.onApplicationEvent(event); } } }
Then in my view class which extends MyAbstractView I implement ApplicationListener, which registers this class in the spring eventCode:public void onApplicationEvent(ApplicationEvent event) { applicationEventHandler.onApplicationEvent(event); }
framework. I then add a listener to handle the OnDeckEvent.
I have found this to meet all my needs. Am I missing out on something by using this simple solution?Code:public class DraftStatusView extends MyAbstractView implements ApplicationListener I can register a listener to the OnDeckEvent. { ... public void initActionCommandExecutor() { getApplicationEventHandler().addApplicationListener(OnDeckEvent.class, new ApplicationListener() { public void onApplicationEvent(ApplicationEvent _event) { OnDeckEvent event = (OnDeckEvent) _event; pick.setText(event.getSelection().getCoach().getName()); secondsLeft = 180; if (!timer.isRunning()) { timer.start(); } } }); }


Reply With Quote