> -----Original Message----- > From: Niclas Hedhman [mailto:[EMAIL PROTECTED] > Sent: Monday, January 19, 2004 8:58 PM > To: Avalon framework users > Subject: Re: [Merlin] Listener best practices > > > On Monday 19 January 2004 11:01, Stephen McConnell wrote: > > Olaf Bergner wrote: > > > Are there any best practices for doing what I am trying to achieve? > > > Currently, I am thinking about writing a lifecycle extension > to that end. > > > Is this a feasible solution? Or even better, does anybody > have a working > > > solution? > > > > Take a look in merlin cvs. > > 1. Does it matter if the Service exposes add/remove listener methods? > 2. Does the Listener explicitly know about the service producing > the events? > 3. If so (no on 1, and yes on 2), I think the JavaBeans approach > is more than > adequate. > > If one want to a weaker coupling, i.e. the listener doesn't know > what sources > exists and the producer doesn't manage its listeners, similar to > what exists > in OSGi. > Basically, for a given EventType one can register itself with the > container as > either a Consumer or Producer. Each doesn't need to know of each > other, and > when an event is fired, the Producer sends it to the container and the > container will distribute it to the Consumers. > I think this is a great pattern!!! > > Can this be implemented without support of the Container?? > > YES. You need to create a Service that is an EventRouter, where > you register > the EventType you want to listen to, and where event producers fire its > events to. At the moment, Merlin doesn't support "lookup all > components that > implement a particular interface" but if it did, the EventRouter > would just > register itself to all known <EventType>Producers. > So, either the producer components needs to know explicitly about this > pattern, or the EventRouter needs to be partly implemented as a LifeCycle > Extension.
Let's assume merlin would have the equivalent of public List lookupImplementors(ServiceInterface.class) What you are proposing is public interface EventProducer { addListener(EventConsumer ec); } public interface EventConsumer { onEvent(Event e); } public class EventRouter implements EventConsumer, Initializable, Serviceable, ... { private List m_eventConsumers; private List m_eventProducers; public service(ServiceManager sm) throws ServiceException { m_eventConsumers = sm.lookupImplementors(EventConsumer.class); m_eventProducers = sm.lookupImplementors(EventProducer.class); for (EventProducer ep in m_eventProducers) { ep.addListener(this); } } public onEvent(Event e) { for (EventConsumer ec in m_eventConsumers) { ec.onEvent(e); } } } Does this capture your ideas? If so, it sounds good to me. Reminds me a bit of the notify() method in JINI's ServiceRegistrar. > > Ideally, we bring this up on the dev@ list and discusses the > introduction into > Merlin on a more permanent basis. > If there's interest in such an extension. Cheers, Olaf --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]