|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.context.event.AbstractApplicationEventMulticaster
public abstract class AbstractApplicationEventMulticaster
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 ApplicationEventMulticaster.multicastEvent(org.springframework.context.ApplicationEvent)
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.
setCollectionClass(java.lang.Class)
,
getApplicationListeners()
,
SimpleApplicationEventMulticaster
Constructor Summary | |
---|---|
AbstractApplicationEventMulticaster()
|
Method Summary | |
---|---|
void |
addApplicationListener(ApplicationListener listener)
Add a listener to be notified of all events. |
protected Collection |
getApplicationListeners()
Return the current Collection of ApplicationListeners. |
void |
removeAllListeners()
Remove all listeners registered with this multicaster. |
void |
removeApplicationListener(ApplicationListener listener)
Remove a listener from the notification list. |
void |
setCollectionClass(Class collectionClass)
Specify the collection class to use. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.springframework.context.event.ApplicationEventMulticaster |
---|
multicastEvent |
Constructor Detail |
---|
public AbstractApplicationEventMulticaster()
Method Detail |
---|
public void setCollectionClass(Class collectionClass)
Default is a linked HashSet, keeping the registration order. If no linked Set implementation is available, a plain HashSet will be used as fallback (not keeping the registration order).
Note that a Set class specified will not permit multiple instances of the same listener, while a List class will allow for registering the same listener multiple times.
Consider Doug Lea's java.util.concurrent.CopyOnWriteArraySet
or its
JDK 1.4 backport, edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet
(or the respective CopyOnWriteArrayList version). Those classes provide a thread-safe
Iterator, optimized for read-mostly usage - matching this use case nicely.
CollectionFactory.createLinkedSetIfPossible(int)
,
CopyOnWriteArraySet
,
CopyOnWriteArraySet
public void addApplicationListener(ApplicationListener listener)
ApplicationEventMulticaster
addApplicationListener
in interface ApplicationEventMulticaster
listener
- the listener to addpublic void removeApplicationListener(ApplicationListener listener)
ApplicationEventMulticaster
removeApplicationListener
in interface ApplicationEventMulticaster
listener
- the listener to removepublic void removeAllListeners()
ApplicationEventMulticaster
removeAllListeners
in interface ApplicationEventMulticaster
protected Collection getApplicationListeners()
Note that this is the raw Collection of ApplicationListeners, potentially modified when new listeners get registered or existing ones get removed. This Collection is not a snapshot copy.
ApplicationListener
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |