public interface EventBus
EventCreator.
In case, a listener that showed interest in the event unregisters before receiving the same, it will not receive this
event. On the other hand, if any new listener gets registered for this event after the initial check is made, it will
receive this event, provided it gets registered before the event starts to get published.
The interest of the listener for this event will only be based on the event type and not the event data as the data is
not available initially. This essentially means that such an event may be filtered-out by the listener based on the
event data when it actually receives the event.Subscribe and having just one argument, the event object itself.
Class.isAssignableFrom(Class) on the event object's class with
the subscriber class as argument returns true, then that subscriber is interested in the event.
SyncSubscribersGatekeeper.ALLOW_SYNC_SUBSCRIBERS is set to false. In that case, the
events will be sent to the subscriber asynchronously. See SyncSubscribersGatekeeper for more details.
There are multiple facets of this async dispatch mode which are described below:
CONSUMER_QUEUE_FULL_RETRY_MAX_DEFAULT which can be overridden by a dynamic property:
CONSUMER_QUEUE_FULL_RETRY_MAX_PROP_NAME. If all the retries fail, the event is rejected.Subscribe.BatchingStrategy. Care must be taken while batching as the
rejections also happens in batch when the subscribers are slow.Subscribe.queueSize(). The default value for this queue size is
CONSUMER_QUEUE_SIZE_DEFAULT which can be changed via the fast property:
CONSUMER_QUEUE_SIZE_DEFAULT_PROP_NAMEcom.netflix.eventbus.filter.lang.
Such a filter can then be converted into an EventFilter using EventFilterCompiler
For relatively easy way of programmatic creation of filters, one can use the utility/factory class
com.netflix.eventbus.filter.EventFilters.DynamicSubscriber is a way to achieve runtime binding of an event
to a subscriber.| Modifier and Type | Field and Description |
|---|---|
static int |
CONSUMER_QUEUE_FULL_RETRY_MAX_DEFAULT |
static java.lang.String |
CONSUMER_QUEUE_FULL_RETRY_MAX_PROP_NAME |
static int |
CONSUMER_QUEUE_SIZE_DEFAULT |
static java.lang.String |
CONSUMER_QUEUE_SIZE_DEFAULT_PROP_NAME |
| Modifier and Type | Method and Description |
|---|---|
void |
addFilterForEvent(EventFilter filter,
java.lang.Class<?> eventClass)
Adds a publisher level filter for the passed event type.
|
void |
addFilterForSubscriber(EventFilter filter,
SubscriberInfo subscriberInfo)
Adds the passed filter for the passed subscriber method.
|
void |
clearFiltersForEvent(java.lang.Class<?> eventClass)
Removes all the filters for the passed event type.
|
void |
clearFiltersForSubscriber(SubscriberInfo subscriberInfo)
Removes all filters for the passed subscriber method.
|
void |
disableCatchAllSubscriber()
Disables the
CatchAllSubscriber for this eventbus. |
boolean |
enableCatchAllSubscriber(java.util.concurrent.BlockingQueue<?> catchAllSink)
Enables the
CatchAllSubscriber for this eventbus with the sink for the subscriber as the passed
BlockingQueue instance. |
java.util.Set<java.lang.Class<?>> |
getAllRegisteredEventTypes()
Returns all the event types which have atleast a subscriber or a filter defined in this event bus.
|
java.util.Set<SubscriberInfo> |
getAllSubscribers()
Returns a set of subscribers that are registered with this event bus.
|
java.util.Set<SubscriberInfo> |
getAllSubscribersForAnEvent(java.lang.Class<?> eventType)
Returns a set of subscribers for that passed event type that are registered with this event bus.
|
java.util.Set<EventFilter> |
getFilterForASubscriber(SubscriberInfo subscriberInfo)
The set of event filters attached to the passed subscriber.
|
java.util.Set<EventFilter> |
getFiltersForAnEvent(java.lang.Class<?> eventType)
The set of event filters attached to the passed event type.
|
void |
publish(java.lang.Object event)
Publishes the passed event object.
|
void |
publishIffNotDead(EventCreator creator,
java.lang.Class<?>... eventTypes)
Publishes events iff there is atleast one listener for any of the passed event types.
|
void |
registerSubscriber(EventFilter filter,
java.lang.Object subscriber)
Registers a subscriber which will be invoked iff the filter passes for the event to be published.
|
void |
registerSubscriber(java.lang.Object subscriber)
Registers a subscriber without any filters.
|
void |
removeFiltersForEvent(java.lang.Class<?> eventClass,
EventFilter... filters)
Removes the passed filters for the passed event type.
|
void |
removeFiltersForSubscriber(SubscriberInfo subscriberInfo,
EventFilter... filters)
Removes the passed filters for the passed subscriber.
|
java.util.Set<java.lang.Object> |
unregisterSubscriber(java.lang.Class<?> subscriberClass)
Removes the subscriber class (all of its subscriber methods) from the event bus.
|
boolean |
unregisterSubscriber(java.lang.Object subscriber)
Removes the subscriber instance (all of its subscriber methods) from the event bus.
|
static final java.lang.String CONSUMER_QUEUE_FULL_RETRY_MAX_PROP_NAME
static final int CONSUMER_QUEUE_FULL_RETRY_MAX_DEFAULT
static final java.lang.String CONSUMER_QUEUE_SIZE_DEFAULT_PROP_NAME
static final int CONSUMER_QUEUE_SIZE_DEFAULT
void publish(java.lang.Object event)
false then exit.event - Event to publish.void publishIffNotDead(EventCreator creator, java.lang.Class<?>... eventTypes)
EventBus
javadocs for details about conditional event publishing. EventCreator.createEvent(java.util.Set) with only the event types that have atleast one listener.false then exit.creator - Event creator to be invoked iff there is atleast one listener for the event types.eventTypes - Event types for which the events are to be published.void registerSubscriber(@Nullable
EventFilter filter,
java.lang.Object subscriber)
throws InvalidSubscriberException
registerSubscriber(Object) and then register each filter using
addFilterForSubscriber(com.netflix.eventbus.spi.EventFilter, com.netflix.eventbus.spi.SubscriberInfo) EventBus javadocs for details about subscribers, filters, event-subscriber association and dispatch
mode.filter - Filter.subscriber - Subscriber to register.InvalidSubscriberException - If the passed subscriber is invalid.void registerSubscriber(java.lang.Object subscriber)
throws InvalidSubscriberException
EventBus javadocs for details about subscribers, filters, event-subscriber association and dispatch
mode.subscriber - Subscriber to register.InvalidSubscriberException - If the passed subscriber is invalid.boolean enableCatchAllSubscriber(java.util.concurrent.BlockingQueue<?> catchAllSink)
CatchAllSubscriber for this eventbus with the sink for the subscriber as the passed
BlockingQueue instance.catchAllSink - The sink for the CatchAllSubscribertrue if the passes sink was successfully attached, iff there is no sink already present.void disableCatchAllSubscriber()
CatchAllSubscriber for this eventbus.java.util.Set<java.lang.Object> unregisterSubscriber(java.lang.Class<?> subscriberClass)
subscriberClass - Subscriber class to unregister.boolean unregisterSubscriber(java.lang.Object subscriber)
subscriber - Subscriber instance to unregister.true if the subscriber instance was unregistered, false otherwise.void addFilterForSubscriber(EventFilter filter, SubscriberInfo subscriberInfo)
EventBus javadocs for details about subscribers & filters.filter - Filter to attach.subscriberInfo - Subscriber info. This can be retrieved by methods getAllSubscribersForAnEvent(Class)
or getAllSubscribers()void removeFiltersForSubscriber(SubscriberInfo subscriberInfo, EventFilter... filters)
EventBus javadocs for details about subscribers & filters.subscriberInfo - Subscriber info. This can be retrieved by methods getAllSubscribersForAnEvent(Class)
or getAllSubscribers()filters - Filters to remove.void clearFiltersForSubscriber(SubscriberInfo subscriberInfo)
EventBus javadocs for details about subscribers & filters.subscriberInfo - Subscriber info. This can be retrieved by methods getAllSubscribersForAnEvent(Class)
or getAllSubscribers()void addFilterForEvent(EventFilter filter, java.lang.Class<?> eventClass)
EventBus javadocs for details about publishers & filters.filter - FiltereventClass - The class of the event for which the filter is to be attached.void removeFiltersForEvent(java.lang.Class<?> eventClass,
EventFilter... filters)
EventBus javadocs for details about publishers & filters.eventClass - The class of the event for which the filter are to be removed.filters - Filters to be removedvoid clearFiltersForEvent(java.lang.Class<?> eventClass)
EventBus javadocs for details about publishers & filters.eventClass - The class of the event for which the filter are to be cleared.java.util.Set<SubscriberInfo> getAllSubscribers()
getFilterForASubscriber(com.netflix.eventbus.spi.SubscriberInfo)java.util.Set<SubscriberInfo> getAllSubscribersForAnEvent(java.lang.Class<?> eventType)
getFilterForASubscriber(SubscriberInfo)eventType - Event for which the subscribers are to be retrieved.java.util.Set<EventFilter> getFilterForASubscriber(SubscriberInfo subscriberInfo)
subscriberInfo - Subscriber info. This can be retrieved by methods getAllSubscribersForAnEvent(Class)
or getAllSubscribers()java.util.Set<EventFilter> getFiltersForAnEvent(java.lang.Class<?> eventType)
eventType - Event type for which the filters are to be retrieved.java.util.Set<java.lang.Class<?>> getAllRegisteredEventTypes()