public class ThreadSafeEventService extends Object
This implementation is not Swing thread-safe. If publication occurs on a thread other than the Swing EventDispatchThread, subscribers will receive the event on the calling thread, and not the EDT. Swing components should use the SwingEventService instead, which is the implementation used by the EventBus.
Two threads may be accessing the ThreadSafeEventService at the same time, one unsubscribing a listener for topic "A" and the other publishing on topic "A". If the unsubscribing thread gets the lock first, then it is unsubscribed, end of story. If the publisher gets the lock first, then a snapshot copy of the current subscribers is made during the publication, the lock is released and the subscribers are called. Between the time the lock is released and the time that the listener is called, the unsubscribing thread can unsubscribe, resulting in an unsubscribed object receiving notification of the event after it was unsubscribed (but just once).
On event publication, subscribers are called in the order in which they subscribed.
Events and/or topic data can be cached, but are not by default. To cache events or topic data, call
setDefaultCacheSizePerClassOrTopic(int)
, setCacheSizeForEventClass(Class, int)
, or
setCacheSizeForTopic(String, int)
, setCacheSizeForTopic(Pattern, int)
. Retrieve cached values
with getLastEvent(Class)
, getLastTopicData(String)
, getCachedEvents(Class)
, or
getCachedTopicData(String)
. Using caching while subscribing
is most likely to make sense only if you subscribe and publish on the same thread (so caching is very useful for
Swing applications since both happen on the EDT in a single-threaded manner). In multithreaded applications, you
never know if your subscriber has handled an event while it was being subscribed (before the subscribe() method
returned) that is newer or older than the retrieved cached value (taken before or after subscribe() respectively).
All logging goes through the Logger
. The Logger is configurable and supports multiple logging systems.
Exceptions are logged by default, override handleException(String,Object,String,Object,Throwable,
StackTraceElement[],String)
to handleException exceptions in another way. Each call to a subscriber is wrapped in
a try block to ensure one listener does not interfere with another.
The EventService may need to clean up stale WeakReferences and ProxySubscribers created for EventBus annotations. (Aside: EventBus Annotations are handled by the creation of proxies to the annotated objects. Since the annotations create weak references by default, annotation proxies must held strongly by the EventService, otherwise the proxy is garbage collected.) When a WeakReference's referent or an ProxySubscriber's proxiedObject (the annotated object) is claimed by the garbage collector, the EventService still holds onto the actual WeakReference or ProxySubscriber subscribed to the EventService (which are pretty tiny).
There are two ways that these stale WeakReferences and ProxySubscribers are cleaned up.
If a topic or class is never published to again, WeakReferences and ProxySubscribers can be left behind if they are not cleaned up. To prevent loitering stale subscribers, the ThreadSafeEventService may periodically run through all the EventSubscribers and VetoSubscribers for all topics and classes and clean up stale proxies. Proxies for Annotations that have a ReferenceStrength.STRONG are never cleaned up in normal usage. (By specifying ReferenceStrength.STRONG, the programmer is buying into unsubscribing annotated objects themselves. There is one caveat: If getProxiedSubscriber() returns null, even for a ProxySubscriber with a STRONG reference strength, that proxy is cleaned up as it is assumed it is stale or just wrong. This would not occur normally in EventBus usage, but only if someone is implementing their own custom ProxySubscriber and/or AnnotationProcessor.)
Cleanup is pretty rare in general. Not only are stale subscribers cleaned up with regular usage, stale subscribers on abandoned topics and classes do not take up a lot of memory, hence, they are allowed to build up to a certain degree. Cleanup does not occur until the number of WeakReferences and SubscriptionsProxy's with WeakReference strength subscribed to an EventService for all the EventService's subscriptions in total exceed the cleanupStartThreshhold, which is set to CLEANUP_START_THRESHOLD_DEFAULT (500) by default. The default is overridable in the constructor or via #setCleanupStartThreshhold(Integer). If set to null, cleanup will never start.
Once the cleanup start threshold is exceeded, a java.util.Timer is started to clean up stale subscribers periodically in another thread. The timer will fire every cleanupPeriodMS milliseconds, which is set to the CLEANUP_PERIOD_MS_DEFAULT (20 minutes) by default. The default is overridable in the constructor or via #setCleanupPeriodMS(Integer). If set to null, cleanup will not start. This is implemented with a java.util.Timer, so Timer's warnings apply - setting this too low will cause cleanups to bunch up and hog the cleanup thread.
After a cleanup cycle completes, if the number of stale subscribers falls at or below the cleanupStopThreshhold cleanup stops until the cleanupStartThreshhold is exceeded again. The cleanupStopThreshhold is set to CLEANUP_STOP_THRESHOLD_DEFAULT (100) by default. The default is overridable in the constructor or via #setCleanupStopThreshhold(Integer). If set to null or 0, cleanup will not stop if it is ever started.
All cleanup parameters are tunable "live" and checked after each subscription and after each cleanup cycle. To make cleanup never run, set cleanupStartThreshhold to Integer.MAX_VALUE and cleanupPeriodMS to null. To get cleanup to run continuously, set set cleanupStartThreshhold to 0 and cleanupPeriodMS to some reasonable value, perhaps 1000 (1 second) or so (not recommended, cleanup is conducted with regular usage and the cleanup thread is rarely created or invoked).
Cleanup is not run in a daemon thread, and thus will not stop the JVM from exiting.
for a complete description of the API
Modifier and Type | Field and Description |
---|---|
static Long |
CLEANUP_PERIOD_MS_DEFAULT |
static Integer |
CLEANUP_START_THRESHOLD_DEFAULT |
static Integer |
CLEANUP_STOP_THRESHOLD_DEFAULT |
protected static org.scijava.event.bushe.Logger |
LOG |
Constructor and Description |
---|
ThreadSafeEventService()
Creates a ThreadSafeEventService that does not monitor timing of handlers.
|
ThreadSafeEventService(Integer cleanupStartThreshold,
Integer cleanupStopThreshold,
Long cleanupPeriodMS)
Creates a ThreadSafeEventService while providing proxy cleanup customization.
|
ThreadSafeEventService(Long timeThresholdForEventTimingEventPublication)
Creates a ThreadSafeEventService while providing time monitoring options.
|
ThreadSafeEventService(Long timeThresholdForEventTimingEventPublication,
Integer cleanupStartThreshold,
Integer cleanupStopThreshold,
Long cleanupPeriodMS)
Creates a ThreadSafeEventService while providing time monitoring options.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addEventToCache(Object event,
String topic,
Object eventObj)
Adds an event to the event cache, if appropriate.
|
void |
clearAllSubscribers()
Clears all current subscribers and veto subscribers
|
void |
clearCache()
Clear all event caches for all topics and event.
|
void |
clearCache(Class eventClassToClear)
Clears the event cache for a specific event class or interface and it's any of it's subclasses or implementing
classes.
|
void |
clearCache(Pattern pattern)
Clears the topic data cache for all topics that match a particular pattern.
|
void |
clearCache(String topic)
Clears the topic data cache for a specific topic name.
|
protected void |
decWeakRefPlusProxySubscriberCount()
Decrement the count of stale proxies
|
List |
getCachedEvents(Class eventClass)
When caching, returns the last set of event published for the type supplied.
|
List |
getCachedTopicData(String topic)
When caching, returns the last set of payload objects published on the topic name supplied.
|
int |
getCacheSizeForEventClass(Class eventClass)
Returns the number of events cached for a particular class of event.
|
int |
getCacheSizeForTopic(String topic)
Returns the number of cached data objects published on a particular topic.
|
Long |
getCleanupPeriodMS()
Get the cleanup interval.
|
Integer |
getCleanupStartThreshhold()
Gets the threshold above which cleanup starts.
|
Integer |
getCleanupStopThreshold()
Gets the threshold below which cleanup stops.
|
int |
getDefaultCacheSizePerClassOrTopic()
The default number of events or payloads kept per event class or topic
|
Object |
getLastEvent(Class eventClass)
When caching, returns the last event publish for the type supplied.
|
Object |
getLastTopicData(String topic)
When caching, returns the last payload published on the topic name supplied.
|
protected Object |
getRealSubscriberAndCleanStaleSubscriberIfNecessary(Iterator iterator,
Object existingSubscriber)
Unsubscribe a subscriber if it is a stale ProxySubscriber.
|
<T> List<T> |
getSubscribers(Class<T> eventClass)
Union of getSubscribersToClass(Class) and getSubscribersToExactClass(Class)
|
<T> List<T> |
getSubscribers(Pattern pattern)
Gets the subscribers that subscribed to a regular expression.
|
<T> List<T> |
getSubscribers(String topic)
Union of getSubscribersByPattern(String) and geSubscribersToTopic(String)
|
<T> List<T> |
getSubscribers(Type eventType)
Gets the subscribers that subscribed to a generic type.
|
<T> List<T> |
getSubscribersByPattern(String topic)
Gets the subscribers that subscribed with a Pattern that matches the given topic.
|
<T> List<T> |
getSubscribersToClass(Class<T> eventClass)
Gets subscribers that subscribed with the given a class, but not those subscribed exactly to the class.
|
<T> List<T> |
getSubscribersToExactClass(Class<T> eventClass)
Gets subscribers that are subscribed exactly to a class, but not those subscribed non-exactly to a class.
|
protected <T> List<T> |
getSubscribersToPattern(Pattern topicPattern) |
<T> List<T> |
getSubscribersToTopic(String topic)
Get the subscribers that subscribed to a topic.
|
<T> List<T> |
getVetoEventListeners(String topicOrPattern)
Union of
getVetoSubscribersToTopic(String) and getVetoSubscribersByPattern(String)
Misnamed method, should be called getVetoSubscribers(String) . |
<T> List<T> |
getVetoSubscribers(Class<T> eventClass)
Gets veto subscribers that subscribed to a given class.
|
<T> List<T> |
getVetoSubscribers(Pattern topicPattern)
Gets the veto subscribers that subscribed to a regular expression.
|
<T> List<T> |
getVetoSubscribers(String topic)
Deprecated.
use getVetoSubscribersToTopic instead for direct replacement,
or use getVetoEventListeners to get topic and pattern matchers.
In EventBus 2.0 this name will replace getVetoEventListeners()
and have it's union functionality
|
<T> List<T> |
getVetoSubscribersByPattern(String pattern)
Gets the veto subscribers that are subscribed by pattern that match the topic.
|
<T> List<T> |
getVetoSubscribersToClass(Class<T> eventClass)
Gets the veto subscribers that subscribed to a class.
|
<T> List<T> |
getVetoSubscribersToExactClass(Class<T> eventClass)
Get veto subscribers that subscribed to a given class exactly.
|
<T> List<T> |
getVetoSubscribersToTopic(String topic)
Gets the veto subscribers that subscribed to a topic.
|
protected void |
handleException(Object event,
Throwable e,
StackTraceElement[] callingStack,
EventSubscriber eventSubscriber)
Called during event handling exceptions, calls handleException
|
protected void |
handleException(String action,
Object event,
String topic,
Object eventObj,
Throwable e,
StackTraceElement[] callingStack,
String sourceString)
All exception handling goes through this method.
|
protected void |
handleVeto(org.scijava.event.bushe.VetoEventListener vl,
Object event,
org.scijava.event.bushe.VetoTopicEventListener vtl,
String topic,
Object eventObj)
Handle vetos of an event or topic, by default logs finely.
|
protected void |
incWeakRefPlusProxySubscriberCount()
Increment the count of stale proxies and start a cleanup task if necessary
|
protected void |
onEventException(String topic,
Object eventObj,
Throwable e,
StackTraceElement[] callingStack,
org.scijava.event.bushe.EventTopicSubscriber eventTopicSubscriber)
Called during event handling exceptions, calls handleException
|
void |
publish(Object event)
Publishes an object so that subscribers will be notified if they subscribed to the object's class, one of its
subclasses, or to one of the interfaces it implements.
|
protected void |
publish(Object event,
String topic,
Object eventObj,
List subscribers,
List vetoSubscribers,
StackTraceElement[] callingStack)
All publish methods call this method.
|
void |
publish(String topicName,
Object eventObj)
Publishes an object on a topic name so that all subscribers to that name or a Regular Expression that matches
the topic name will be notified.
|
void |
publish(Type genericType,
Object event)
Use this method to publish generified objects to subscribers of Types, i.e.
|
protected void |
removeProxySubscriber(org.scijava.event.bushe.ProxySubscriber proxy,
Iterator iter) |
void |
setCacheSizeForEventClass(Class eventClass,
int cacheSize)
Set the number of events cached for a particular class of event.
|
void |
setCacheSizeForTopic(Pattern pattern,
int cacheSize)
Set the number of published data objects cached for topics matching a pattern.
|
void |
setCacheSizeForTopic(String topicName,
int cacheSize)
Set the number of published data objects cached for a particular event topic.
|
void |
setCleanupPeriodMS(Long cleanupPeriodMS)
Sets the cleanup interval.
|
void |
setCleanupStartThreshhold(Integer cleanupStartThreshhold)
Sets the threshold above which cleanup starts.
|
void |
setCleanupStopThreshold(Integer cleanupStopThreshold)
Sets the threshold below which cleanup stops.
|
void |
setDefaultCacheSizePerClassOrTopic(int defaultCacheSizePerClassOrTopic)
Sets the default cache size for each kind of event, default is 0 (no caching).
|
protected void |
setStatus(org.scijava.event.bushe.PublicationStatus status,
Object event,
String topic,
Object eventObj)
Called during publication to set the status on an event.
|
boolean |
subscribe(Class cl,
EventSubscriber eh)
Subscribes an EventSubscriber to the publication of objects matching a type.
|
protected boolean |
subscribe(Object classTopicOrPatternWrapper,
Map<Object,Object> subscriberMap,
Object subscriber)
All subscribe methods call this method, including veto subscriptions.
|
boolean |
subscribe(Pattern pat,
org.scijava.event.bushe.EventTopicSubscriber eh)
Subscribes an EventSubscriber to the publication of all the topic names that match a RegEx Pattern.
|
boolean |
subscribe(String topic,
org.scijava.event.bushe.EventTopicSubscriber eh)
Subscribes an EventTopicSubscriber to the publication of a topic name.
|
boolean |
subscribe(Type type,
EventSubscriber eh)
Subscribe an EventSubscriber to publication of generic Types.
|
boolean |
subscribeExactly(Class cl,
EventSubscriber eh)
Subscribes an EventSubscriber to the publication of objects exactly matching a type.
|
boolean |
subscribeExactlyStrongly(Class cl,
EventSubscriber eh)
Subscribes an EventSubscriber to the publication of objects matching a type exactly.
|
boolean |
subscribeStrongly(Class cl,
EventSubscriber eh)
Subscribes an EventSubscriber to the publication of objects matching a type.
|
boolean |
subscribeStrongly(Pattern pat,
org.scijava.event.bushe.EventTopicSubscriber eh)
Subscribes a subscriber to all the event topic names that match a RegEx expression.
|
boolean |
subscribeStrongly(String name,
org.scijava.event.bushe.EventTopicSubscriber eh)
Subscribes a subscriber to an event topic name.
|
protected void |
subscribeVetoException(Object event,
String topic,
Object eventObj,
Throwable e,
StackTraceElement[] callingStack,
org.scijava.event.bushe.VetoEventListener vetoer)
Called during veto exceptions, calls handleException
|
boolean |
subscribeVetoListener(Class eventClass,
org.scijava.event.bushe.VetoEventListener vetoListener)
Subscribes a VetoEventListener to publication of event matching a class.
|
protected boolean |
subscribeVetoListener(Object subscription,
Map vetoListenerMap,
Object vetoListener)
All veto subscriptions methods call this method.
|
boolean |
subscribeVetoListener(Pattern topicPattern,
org.scijava.event.bushe.VetoTopicEventListener vetoListener)
Subscribes an VetoTopicEventListener to all the topic names that match the RegEx Pattern.
|
boolean |
subscribeVetoListener(String topic,
org.scijava.event.bushe.VetoTopicEventListener vetoListener)
Subscribes a VetoTopicEventListener to a topic name.
|
boolean |
subscribeVetoListenerExactly(Class eventClass,
org.scijava.event.bushe.VetoEventListener vetoListener)
Subscribes a VetoEventListener to publication of an exact event class.
|
boolean |
subscribeVetoListenerExactlyStrongly(Class eventClass,
org.scijava.event.bushe.VetoEventListener vetoListener)
Subscribes a VetoEventListener for an event class (but not its subclasses).
|
boolean |
subscribeVetoListenerStrongly(Class eventClass,
org.scijava.event.bushe.VetoEventListener vetoListener)
Subscribes a VetoEventListener for an event class and its subclasses.
|
boolean |
subscribeVetoListenerStrongly(Pattern topicPattern,
org.scijava.event.bushe.VetoTopicEventListener vetoListener)
Subscribes a VetoTopicEventListener to a set of topics that match a RegEx expression.
|
boolean |
subscribeVetoListenerStrongly(String topic,
org.scijava.event.bushe.VetoTopicEventListener vetoListener)
Subscribes a VetoEventListener to a topic name.
|
boolean |
unsubscribe(Class cl,
EventSubscriber eh)
Stop the subscription for a subscriber that is subscribed to a class.
|
boolean |
unsubscribe(Class eventClass,
Object subscribedByProxy)
Stop a subscription for an object that is subscribed with a ProxySubscriber.
|
protected boolean |
unsubscribe(Object o,
Map subscriberMap,
Object subscriber)
All event subscriber unsubscriptions call this method.
|
boolean |
unsubscribe(Pattern topicPattern,
org.scijava.event.bushe.EventTopicSubscriber eh)
Stop the subscription for a subscriber that is subscribed to event topics via a Pattern.
|
boolean |
unsubscribe(Pattern pattern,
Object subscribedByProxy)
When using annotations, an object may be subscribed by proxy.
|
boolean |
unsubscribe(String name,
org.scijava.event.bushe.EventTopicSubscriber eh)
Stop the subscription for a subscriber that is subscribed to an event topic.
|
boolean |
unsubscribe(String topic,
Object subscribedByProxy)
Stop a subscription for an object that is subscribed to a topic with a ProxySubscriber.
|
boolean |
unsubscribeExactly(Class cl,
EventSubscriber eh)
Stop the subscription for a subscriber that is subscribed to an exact class.
|
boolean |
unsubscribeExactly(Class eventClass,
Object subscribedByProxy)
Stop a subscription for an object that is subscribed exactly with a ProxySubscriber.
|
boolean |
unsubscribeVeto(Class eventClass,
Object subscribedByProxy)
Stop a veto subscription for an object that is subscribed with a ProxySubscriber.
|
boolean |
unsubscribeVeto(Pattern pattern,
Object subscribedByProxy)
When using annotations, an object may be subscribed by proxy.
|
boolean |
unsubscribeVeto(String topic,
Object subscribedByProxy)
Stop a veto subscription for an object that is subscribed to a topic with a ProxySubscriber.
|
boolean |
unsubscribeVetoExactly(Class eventClass,
Object subscribedByProxy)
Stop a veto subscription for an object that is subscribed exactly with a ProxySubscriber.
|
boolean |
unsubscribeVetoListener(Class eventClass,
org.scijava.event.bushe.VetoEventListener vetoListener)
Stop the subscription for a vetoListener that is subscribed to an event class and its subclasses.
|
protected boolean |
unsubscribeVetoListener(Object o,
Map vetoListenerMap,
Object vl)
All veto unsubscriptions methods call this method.
|
boolean |
unsubscribeVetoListener(Pattern topicPattern,
org.scijava.event.bushe.VetoTopicEventListener vetoListener)
Stop the subscription for a VetoTopicEventListener that is subscribed to an event topic RegEx pattern.
|
boolean |
unsubscribeVetoListener(String topic,
org.scijava.event.bushe.VetoTopicEventListener vetoListener)
Stop the subscription for a VetoTopicEventListener that is subscribed to an event topic name.
|
boolean |
unsubscribeVetoListenerExactly(Class eventClass,
org.scijava.event.bushe.VetoEventListener vetoListener)
Stop the subscription for a vetoListener that is subscribed to an event class (but not its subclasses).
|
public static final Integer CLEANUP_START_THRESHOLD_DEFAULT
public static final Integer CLEANUP_STOP_THRESHOLD_DEFAULT
public static final Long CLEANUP_PERIOD_MS_DEFAULT
protected static final org.scijava.event.bushe.Logger LOG
public ThreadSafeEventService()
public ThreadSafeEventService(Long timeThresholdForEventTimingEventPublication)
timeThresholdForEventTimingEventPublication
- the longest time a subscriber should spend handling an event,
The service will publish an SubscriberTimingEvent after listener processing if the time was exceeded. If null, no
EventSubscriberTimingEvent will be issued.public ThreadSafeEventService(Integer cleanupStartThreshold, Integer cleanupStopThreshold, Long cleanupPeriodMS)
cleanupStartThreshold
- see class javadoc.cleanupStopThreshold
- see class javadoc.cleanupPeriodMS
- see class javadoc.public ThreadSafeEventService(Long timeThresholdForEventTimingEventPublication, Integer cleanupStartThreshold, Integer cleanupStopThreshold, Long cleanupPeriodMS)
timeThresholdForEventTimingEventPublication
- the longest time a subscriber should spend handling an event.
The service will publish an SubscriberTimingEvent after listener processing if the time was exceeded. If null, no
SubscriberTimingEvent will be issued.cleanupStartThreshold
- see class javadoc.cleanupStopThreshold
- see class javadoc.cleanupPeriodMS
- see class javadoc.public Integer getCleanupStartThreshhold()
public void setCleanupStartThreshhold(Integer cleanupStartThreshhold)
cleanupStartThreshhold
- threshold at which cleanup startspublic Integer getCleanupStopThreshold()
public void setCleanupStopThreshold(Integer cleanupStopThreshold)
cleanupStopThreshold
- threshold at which cleanup stops (it may start again).public Long getCleanupPeriodMS()
public void setCleanupPeriodMS(Long cleanupPeriodMS)
cleanupPeriodMS
- interval in milliseconds between cleanup runs. Passing null
stops cleanup.public boolean subscribe(Class cl, EventSubscriber eh)
Subscribing to a class means the subscriber will be called when objects of that class are published, when objects of subclasses of the class are published, when objects implementing any of the interfaces of the class are published, or when generic types are published with the class' raw type.
Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then onEvent(Object) will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.
It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.
The service will create the WeakReference on behalf of the caller.
cl
- the class of published objects to subscriber listen toeh
- The subscriber that will accept the events of the event class when published.EventService.subscribe(Class,EventSubscriber)
public boolean subscribe(Type type, EventSubscriber eh)
publish(java.lang.reflect.Type, Object)
.
Due to generic type erasure, the type must be supplied by the publisher. You can get a declared object's
type by using the TypeReference
class. For Example:
TypeReference<List<Trade>> subscribingTypeReference = new TypeReference<List<Trade>>(){}; EventBus.subscribe(subscribingTypeReference.getType(), mySubscriber); EventBus.subscribe(List.class, thisSubscriberWillGetCalledToo); ... //Likely in some other class TypeReference<List<Trade>> publishingTypeReference = new TypeReference<List<Trade>>(){}; List<Trade> trades = new ArrayList<Trade>(); EventBus.publish(publishingTypeReference.getType(), trades); trades.add(trade); EventBus.publish(publishingTypeReference.getType(), trades);
type
- the generic type to subscribe toeh
- the subscriber to the typeEventService.subscribe(java.lang.reflect.Type, EventSubscriber)
public boolean subscribeExactly(Class cl, EventSubscriber eh)
Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.
It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.
The service will create the WeakReference on behalf of the caller.
cl
- the class of published objects to listen toeh
- The subscriber that will accept the events when published.EventService.subscribeExactly(Class,EventSubscriber)
public boolean subscribe(String topic, org.scijava.event.bushe.EventTopicSubscriber eh)
Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.
It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.
topic
- the name of the topic listened toeh
- The topic subscriber that will accept the events when published.EventService.subscribe(String,EventTopicSubscriber)
public boolean subscribe(Pattern pat, org.scijava.event.bushe.EventTopicSubscriber eh)
Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.
It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.
pat
- pattern that matches to the name of the topic published toeh
- The topic subscriber that will accept the events when published.EventService.subscribe(Pattern,EventTopicSubscriber)
public boolean subscribeStrongly(Class cl, EventSubscriber eh)
The semantics are the same as subscribe(Class, EventSubscriber)
, except that the EventService holds
a regularly reference, not a WeakReference.
The subscriber will remain subscribed until unsubscribe(Class,EventSubscriber)
is called.
cl
- the class of published objects to listen toeh
- The subscriber that will accept the events when published.EventService.subscribeStrongly(Class,EventSubscriber)
public boolean subscribeExactlyStrongly(Class cl, EventSubscriber eh)
The semantics are the same as subscribeExactly(Class, EventSubscriber)
, except that the EventService
holds a regularly reference, not a WeakReference.
The subscriber will remain subscribed until unsubscribe(Class,EventSubscriber)
is called.
cl
- the class of published objects to listen toeh
- The subscriber that will accept the events when published.EventService.subscribeExactlyStrongly(Class,EventSubscriber)
public boolean subscribeStrongly(String name, org.scijava.event.bushe.EventTopicSubscriber eh)
The semantics are the same as subscribe(String, EventTopicSubscriber)
, except that the EventService
holds a regularly reference, not a WeakReference.
The subscriber will remain subscribed until unsubscribe(String,EventTopicSubscriber)
is called.
name
- the name of the topic listened toeh
- The topic subscriber that will accept the events when published.EventService.subscribeStrongly(String,EventTopicSubscriber)
public boolean subscribeStrongly(Pattern pat, org.scijava.event.bushe.EventTopicSubscriber eh)
The semantics are the same as subscribe(java.util.regex.Pattern, EventTopicSubscriber)
, except that the
EventService holds a regularly reference, not a WeakReference.
The subscriber will remain subscribed until unsubscribe(String,EventTopicSubscriber)
is called.
pat
- the name of the topic listened toeh
- The topic subscriber that will accept the events when published.EventService.subscribeStrongly(Pattern,EventTopicSubscriber)
public void clearAllSubscribers()
EventService.clearAllSubscribers()
public boolean subscribeVetoListener(Class eventClass, org.scijava.event.bushe.VetoEventListener vetoListener)
Use this method to avoid having to call unsubscribe(), though with care since garbage collection semantics is indeterminate. The service will respect the WeakReference semantics. In other words, if the vetoListener has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.
It's allowable to call unsubscribe() with the same VetoEventListener hard reference to stop a subscription immediately.
The service will create the WeakReference on behalf of the caller.
eventClass
- the class of published objects that can be vetoedvetoListener
- The VetoEventListener that can determine whether an event is published.EventService.subscribeVetoListener(Class,VetoEventListener)
public boolean subscribeVetoListenerExactly(Class eventClass, org.scijava.event.bushe.VetoEventListener vetoListener)
Use this method to avoid having to call unsubscribe(), though with care since garbage collection semantics is indeterminate. The service will respect the WeakReference semantics. In other words, if the vetoListener has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.
It's allowable to call unsubscribe() with the same VetoEventListener hard reference to stop a subscription immediately.
The service will create the WeakReference on behalf of the caller.
eventClass
- the class of published objects that can be vetoedvetoListener
- The vetoListener that can determine whether an event is published.EventService.subscribeVetoListenerExactly(Class,VetoEventListener)
public boolean subscribeVetoListener(String topic, org.scijava.event.bushe.VetoTopicEventListener vetoListener)
topic
- the name of the topic listened tovetoListener
- The vetoListener that can determine whether an event is published.EventService.subscribeVetoListener(String,VetoTopicEventListener)
public boolean subscribeVetoListener(Pattern topicPattern, org.scijava.event.bushe.VetoTopicEventListener vetoListener)
topicPattern
- the RegEx pattern to match topics withvetoListener
- The vetoListener that can determine whether an event is published.EventService.subscribeVetoListener(Pattern,VetoTopicEventListener)
public boolean subscribeVetoListenerStrongly(Class eventClass, org.scijava.event.bushe.VetoEventListener vetoListener)
The VetoEventListener will remain subscribed until unsubscribeVetoListener(Class,VetoEventListener)
is
called.
eventClass
- the class of published objects to listen tovetoListener
- The vetoListener that will accept the events when published.EventService.subscribeVetoListenerStrongly(Class,VetoEventListener)
public boolean subscribeVetoListenerExactlyStrongly(Class eventClass, org.scijava.event.bushe.VetoEventListener vetoListener)
The VetoEventListener will remain subscribed until unsubscribeVetoListener(Class,VetoEventListener)
is
called.
eventClass
- the class of published objects to listen tovetoListener
- The vetoListener that will accept the events when published.EventService.subscribeVetoListenerExactlyStrongly(Class,VetoEventListener)
public boolean subscribeVetoListenerStrongly(String topic, org.scijava.event.bushe.VetoTopicEventListener vetoListener)
The VetoEventListener will remain subscribed until unsubscribeVetoListener(String,VetoTopicEventListener)
is
called.
topic
- the name of the topic listened tovetoListener
- The topic vetoListener that will accept or reject publication.EventService.subscribeVetoListenerStrongly(String,VetoTopicEventListener)
public boolean subscribeVetoListenerStrongly(Pattern topicPattern, org.scijava.event.bushe.VetoTopicEventListener vetoListener)
The VetoEventListener will remain subscribed until unsubscribeVetoListener(Pattern,VetoTopicEventListener)
is
called.
topicPattern
- the RegEx pattern that matches the name of the topics listened tovetoListener
- The topic vetoListener that will accept or reject publication.EventService.subscribeVetoListenerStrongly(Pattern,VetoTopicEventListener)
protected boolean subscribeVetoListener(Object subscription, Map vetoListenerMap, Object vetoListener)
subscription
- the topic, Pattern, or event class to subscribe tovetoListenerMap
- the internal map of veto listeners to use (by topic of class)vetoListener
- the veto listener to subscribe, may be a VetoEventListener or a WeakReference to oneIllegalArgumentException
- if vl or o is nullprotected boolean subscribe(Object classTopicOrPatternWrapper, Map<Object,Object> subscriberMap, Object subscriber)
Overriding this method is only for the adventurous. This basically gives you just enough rope to hang yourself.
classTopicOrPatternWrapper
- the topic String, event Class, or PatternWrapper to subscribe tosubscriberMap
- the internal map of subscribers to use (by topic or class)subscriber
- the EventSubscriber or EventTopicSubscriber to subscribe, or a WeakReference to eitherIllegalArgumentException
- if subscriber or topicOrClass is nullpublic boolean unsubscribe(Class cl, EventSubscriber eh)
cl
- the class of published objects to listen toeh
- The subscriber that is subscribed to the event. The same reference as the one subscribed.EventService.unsubscribe(Class,EventSubscriber)
public boolean unsubscribeExactly(Class cl, EventSubscriber eh)
cl
- the class of published objects to listen toeh
- The subscriber that is subscribed to the event. The same reference as the one subscribed.EventService.unsubscribeExactly(Class,EventSubscriber)
public boolean unsubscribe(String name, org.scijava.event.bushe.EventTopicSubscriber eh)
name
- the topic listened toeh
- The subscriber that is subscribed to the topic. The same reference as the one subscribed.EventService.unsubscribe(String,EventTopicSubscriber)
public boolean unsubscribe(Pattern topicPattern, org.scijava.event.bushe.EventTopicSubscriber eh)
topicPattern
- the regex expression matching topics listened toeh
- The subscriber that is subscribed to the topic. The same reference as the one subscribed.EventService.unsubscribe(String,EventTopicSubscriber)
public boolean unsubscribe(Class eventClass, Object subscribedByProxy)
If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
eventClass
- class this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribe(Class,Object)
public boolean unsubscribeExactly(Class eventClass, Object subscribedByProxy)
If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
eventClass
- class this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribeExactly(Class,Object)
public boolean unsubscribe(String topic, Object subscribedByProxy)
If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
topic
- the topic this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribe(String,Object)
public boolean unsubscribe(Pattern pattern, Object subscribedByProxy)
If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
pattern
- the RegEx expression this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribe(java.util.regex.Pattern,Object)
protected boolean unsubscribe(Object o, Map subscriberMap, Object subscriber)
o
- the topic or event class to unsubscribe fromsubscriberMap
- the map of subscribers to use (by topic of class)subscriber
- the subscriber to unsubscribe, either an EventSubscriber or an EventTopicSubscriber, or a WeakReference
to eitherpublic boolean unsubscribeVeto(Class eventClass, Object subscribedByProxy)
If an object is subscribed by proxy and it implements VetoSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
eventClass
- class this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribeVeto(Class,Object)
public boolean unsubscribeVetoExactly(Class eventClass, Object subscribedByProxy)
If an object is subscribed by proxy and it implements VetoSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
eventClass
- class this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribeVetoExactly(Class,Object)
public boolean unsubscribeVeto(String topic, Object subscribedByProxy)
If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
topic
- the topic this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribeVeto(String,Object)
public boolean unsubscribeVeto(Pattern pattern, Object subscribedByProxy)
If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.
pattern
- the RegEx expression this object is subscribed to by proxysubscribedByProxy
- object subscribed by proxyEventService.unsubscribeVeto(java.util.regex.Pattern,Object)
public boolean unsubscribeVetoListener(Class eventClass, org.scijava.event.bushe.VetoEventListener vetoListener)
eventClass
- the class of published objects that can be vetoedvetoListener
- The vetoListener that will accept or reject publication of an event.EventService.unsubscribeVetoListener(Class,VetoEventListener)
public boolean unsubscribeVetoListenerExactly(Class eventClass, org.scijava.event.bushe.VetoEventListener vetoListener)
eventClass
- the class of published objects that can be vetoedvetoListener
- The vetoListener that will accept or reject publication of an event.EventService.unsubscribeVetoListenerExactly(Class,VetoEventListener)
public boolean unsubscribeVetoListener(String topic, org.scijava.event.bushe.VetoTopicEventListener vetoListener)
topic
- the name of the topic that is listened tovetoListener
- The vetoListener that can determine whether an event is published on that topicEventService.unsubscribeVetoListener(String,VetoTopicEventListener)
public boolean unsubscribeVetoListener(Pattern topicPattern, org.scijava.event.bushe.VetoTopicEventListener vetoListener)
topicPattern
- the RegEx pattern matching the name of the topics listened tovetoListener
- The vetoListener that can determine whether an event is published on that topicEventService.unsubscribeVetoListener(Pattern,VetoTopicEventListener)
protected boolean unsubscribeVetoListener(Object o, Map vetoListenerMap, Object vl)
o
- the topic or event class to unsubscribe fromvetoListenerMap
- the map of veto listeners to use (by topic or class)vl
- the veto listener to unsubscribe, or a WeakReference to onepublic void publish(Object event)
event
- the object to publishEventService.publish(Object)
public void publish(Type genericType, Object event)
subscribe(Type, EventSubscriber)
, and to publish to subscribers of the non-generic type.
Due to generic type erasure, the type must be supplied by the caller. You can get a declared object's
type by using the TypeReference
class. For Example:
TypeReference<List<Trade>> subscribingTypeReference = new TypeReference<List<Trade>>(){}; EventBus.subscribe(subscribingTypeReference.getType(), mySubscriber); EventBus.subscribe(List.class, thisSubscriberWillGetCalledToo); ... //Likely in some other class TypeReference<List<Trade>> publishingTypeReference = new TypeReference<List<Trade>>(){}; List<Trade> trades = new ArrayList<Trade>(); EventBus.publish(publishingTypeReference.getType(), trades); trades.add(trade); EventBus.publish(publishingTypeReference.getType(), trades);
genericType
- the generified type of the published object.event
- The event that occurredEventService.publish(java.lang.reflect.Type, Object)
public void publish(String topicName, Object eventObj)
topicName
- The name of the topic subscribed toeventObj
- the object to publishEventService.publish(String,Object)
protected void publish(Object event, String topic, Object eventObj, List subscribers, List vetoSubscribers, StackTraceElement[] callingStack)
event
- the event to publish, null if publishing on a topictopic
- if publishing on a topic, the topic to publish on, else nulleventObj
- if publishing on a topic, the eventObj to publish, else nullsubscribers
- the subscribers to publish to - must be a snapshot copyvetoSubscribers
- the veto subscribers to publish to - must be a snapshot copy.callingStack
- the stack that called this publication, helpful for reporting errors on other threadsIllegalArgumentException
- if eh or o is nullprotected void setStatus(org.scijava.event.bushe.PublicationStatus status, Object event, String topic, Object eventObj)
status
- the status to set on the objectevent
- the event being published, will be null if topic is not nulltopic
- the topic eventObj is being published on, will be null if event is not nulleventObj
- the payload being published on the topic , will be null if event is not nullprotected void addEventToCache(Object event, String topic, Object eventObj)
Using protected visibility to open the caching to other implementations.
event
- the event about to be published, null if topic is non-nulltopic
- the topic about to be published to, null if the event is non-nulleventObj
- the eventObj about to be published on a topic, null if the event is non-nullpublic <T> List<T> getSubscribers(Class<T> eventClass)
eventClass
- the eventClass of interestEventService.getSubscribers(Class)
public <T> List<T> getSubscribersToClass(Class<T> eventClass)
eventClass
- the eventClass of interestEventService.getSubscribersToClass(Class)
public <T> List<T> getSubscribersToExactClass(Class<T> eventClass)
eventClass
- the eventClass of interestEventService.getSubscribersToExactClass(Class)
public <T> List<T> getSubscribers(Type eventType)
eventType
- the type of interestEventService.getSubscribers(Type)
public <T> List<T> getSubscribers(String topic)
topic
- the topic of interestEventService.getSubscribers(String)
public <T> List<T> getSubscribersToTopic(String topic)
topic
- the topic of interestEventService.getSubscribersToTopic(String)
public <T> List<T> getSubscribers(Pattern pattern)
pattern
- the RegEx pattern that was subscribed toEventService.getSubscribers(Pattern)
public <T> List<T> getSubscribersByPattern(String topic)
topic
- a topic to match Patterns againstEventService.getSubscribersByPattern(String)
public <T> List<T> getVetoSubscribers(Class<T> eventClass)
eventClass
- the eventClass of interestEventService.getVetoSubscribers(Class)
public <T> List<T> getVetoSubscribersToClass(Class<T> eventClass)
eventClass
- the eventClass of interestEventService.getVetoSubscribersToClass(Class)
public <T> List<T> getVetoSubscribersToExactClass(Class<T> eventClass)
eventClass
- the eventClass of interestEventService.getVetoSubscribersToExactClass(Class)
public <T> List<T> getVetoEventListeners(String topicOrPattern)
getVetoSubscribersToTopic(String)
and getVetoSubscribersByPattern(String)
Misnamed method, should be called getVetoSubscribers(String)
. Will be deprecated in 1.5.topicOrPattern
- the topic or pattern of interestEventService.getVetoEventListeners(String)
public <T> List<T> getVetoSubscribersToTopic(String topic)
topic
- the topic of interestEventService.getVetoSubscribersToTopic(String)
public <T> List<T> getVetoSubscribers(String topic)
topic
- the topic exactly subscribed toEventService.getVetoSubscribersToTopic(String)
public <T> List<T> getVetoSubscribers(Pattern topicPattern)
topicPattern
- the RegEx pattern for the topic of interestEventService.getVetoSubscribers(Pattern)
public <T> List<T> getVetoSubscribersByPattern(String pattern)
pattern
- the topic to match the pattern string subscribed toEventService.getVetoSubscribersByPattern(String)
protected void handleVeto(org.scijava.event.bushe.VetoEventListener vl, Object event, org.scijava.event.bushe.VetoTopicEventListener vtl, String topic, Object eventObj)
vl
- the veto listener for an eventevent
- the event, can be null if topic is notvtl
- the veto listener for a topictopic
- can be null if event is noteventObj
- the object published with the topicpublic void setDefaultCacheSizePerClassOrTopic(int defaultCacheSizePerClassOrTopic)
If this value is set to a positive number, then when an event is published, the EventService caches the event or topic payload data for later retrieval. This allows subscribers to find out what has most recently happened before they subscribed. The cached event(s) are returned from #getLastEvent(Class), #getLastTopicData(String), #getCachedEvents(Class), or #getCachedTopicData(String)
The default can be overridden on a by-event-class or by-topic basis.
defaultCacheSizePerClassOrTopic
- public int getDefaultCacheSizePerClassOrTopic()
public void setCacheSizeForEventClass(Class eventClass, int cacheSize)
This overrides any setting for the DefaultCacheSizePerClassOrTopic.
Class hierarchy semantics are respected. That is, if there are three events, A, X and Y, and X and Y are both derived from A, then setting the cache size for A applies the cache size for all three. Setting the cache size for X applies to X and leaves the settings for A and Y in tact. Interfaces can be passed to this method, but they only take effect if the cache size of a class or it's superclasses has been set. Just like Class.getInterfaces(), if multiple cache sizes are set, the interface names declared earliest in the implements clause of the eventClass takes effect.
The cache for an event is not adjusted until the next event of that class is published.
eventClass
- the class of eventcacheSize
- the number of published events to cache for this eventpublic int getCacheSizeForEventClass(Class eventClass)
This result is computed for a particular class from the values passed to #setCacheSizeForEventClass(Class, int), and respects the class hierarchy.
eventClass
- the class of eventsetCacheSizeForEventClass(Class,int)
public void setCacheSizeForTopic(String topicName, int cacheSize)
This overrides any setting for the DefaultCacheSizePerClassOrTopic.
Settings for exact topic names take precedence over pattern matching.
The cache for a topic is not adjusted until the next publication on that topic.
topicName
- the topic namecacheSize
- the number of published data Objects to cache for this topicpublic void setCacheSizeForTopic(Pattern pattern, int cacheSize)
This overrides any setting for the DefaultCacheSizePerClassOrTopic.
Settings for exact topic names take precedence over pattern matching. If a topic matches the cache settings for more than one pattern, the cache size chosen is an undetermined one from one of the matched pattern settings.
The cache for a topic is not adjusted until the next publication on that topic.
pattern
- the pattern matching topic namescacheSize
- the number of data Objects to cache for this topicpublic int getCacheSizeForTopic(String topic)
This result is computed for a particular topic from the values passed to #setCacheSizeForTopic(String, int) and #setCacheSizeForTopic(Pattern, int).
topic
- the topic namesetCacheSizeForTopic(String,int)
,
setCacheSizeForTopic(java.util.regex.Pattern,int)
public Object getLastEvent(Class eventClass)
eventClass
- an index into the cache, cannot be an interfacepublic List getCachedEvents(Class eventClass)
eventClass
- an index into the cache, cannot be an interfacepublic Object getLastTopicData(String topic)
topic
- an index into the cachepublic List getCachedTopicData(String topic)
topic
- an index into the cachepublic void clearCache(Class eventClassToClear)
eventClassToClear
- the event class to clear the cache forpublic void clearCache(String topic)
topic
- the topic name to clear the cache forpublic void clearCache(Pattern pattern)
pattern
- the pattern to match topic caches topublic void clearCache()
protected void subscribeVetoException(Object event, String topic, Object eventObj, Throwable e, StackTraceElement[] callingStack, org.scijava.event.bushe.VetoEventListener vetoer)
protected void onEventException(String topic, Object eventObj, Throwable e, StackTraceElement[] callingStack, org.scijava.event.bushe.EventTopicSubscriber eventTopicSubscriber)
protected void handleException(Object event, Throwable e, StackTraceElement[] callingStack, EventSubscriber eventSubscriber)
protected void handleException(String action, Object event, String topic, Object eventObj, Throwable e, StackTraceElement[] callingStack, String sourceString)
protected Object getRealSubscriberAndCleanStaleSubscriberIfNecessary(Iterator iterator, Object existingSubscriber)
Not private since I don't claim I'm smart enough to anticipate all needs, but I am smart enough to doc the rules you must follow to override this method. Those rules may change (changes will be doc'ed), override at your own risk.
Overriders MUST call iterator.remove() to unsubscribe the proxy if the subscriber is a ProxySubscriber and is stale and should be cleaned up. If the ProxySubscriber is unsubscribed, then implementers MUST also call proxyUnsubscribed() on the subscriber. Overriders MUST also remove the proxy from the weakProxySubscriber list by calling removeStaleProxyFromList. Method assumes caller is holding the listenerList lock (else how can you pass the iterator?).
iterator
- current iteratorexistingSubscriber
- the current value of the iteratorprotected void removeProxySubscriber(org.scijava.event.bushe.ProxySubscriber proxy, Iterator iter)
protected void incWeakRefPlusProxySubscriberCount()
protected void decWeakRefPlusProxySubscriberCount()
Copyright © 2009–2024 SciJava. All rights reserved.