Abstract implementation of the ApplicationEventMulticaster interface,
providing the basic listener registration facility.
Doesn't permit multiple instances of the same listener by default,
as it keeps listeners in a linked Set. The collection class used to hold
ApplicationListener objects can be overridden through the "collectionClass"
bean property.
Note that this class doesn't try to do anything clever to ensure thread
safety if listeners are added or removed at runtime. A technique such as
Copy-on-Write (Lea:137) could be used to ensure this, but the assumption in
the basic version of the class is that listeners will be added at application
configuration time and not added or removed as the application runs.
A custom collection class must be specified to allow for thread-safe
runtime registration of listeners. A good candidate for this is Doug Lea's
java.util.concurrent.CopyOnWriteArraySet or its non-JDK predecessor,
EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet (or the
respective CopyOnWriteArrayList version, allowing for registering the same
listener multiple times). Those classes provide a thread-safe Iterator,
optimized for read-mostly usage - matching this use case nicely.
Implementing ApplicationEventMulticaster's actual multicastEvent
method is left to subclasses. SimpleApplicationEventMulticaster simply multicasts
all events to all registered listeners, invoking them in the calling thread.
Alternative implementations could be more sophisticated in those respects.
author: Juergen Hoeller since: 1.2.3 See Also: AbstractApplicationEventMulticaster.setCollectionClass See Also: AbstractApplicationEventMulticaster.getApplicationListeners() See Also: SimpleApplicationEventMulticaster |