com.netflix.eventbus.impl
Class EventBusImpl

java.lang.Object
  extended by com.netflix.eventbus.impl.EventBusImpl
All Implemented Interfaces:
EventBus

public class EventBusImpl
extends java.lang.Object
implements EventBus

An implementation of EventBus. This implementation is based on the google eventbus com.google.common.eventbus.EventBus but the absence of descent extension points forces us to inspire but not extend that implementation. The critical parts that drove us towards this approach are:


Field Summary
 
Fields inherited from interface com.netflix.eventbus.spi.EventBus
CONSUMER_QUEUE_FULL_RETRY_MAX_DEFAULT, CONSUMER_QUEUE_FULL_RETRY_MAX_PROP_NAME, CONSUMER_QUEUE_SIZE_DEFAULT, CONSUMER_QUEUE_SIZE_DEFAULT_PROP_NAME
 
Constructor Summary
EventBusImpl()
           
 
Method Summary
 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.
 void shutdown()
           
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EventBusImpl

public EventBusImpl()
Method Detail

publish

public void publish(java.lang.Object event)
Description copied from interface: EventBus
Publishes the passed event object. The following is done on receiving this call:

Specified by:
publish in interface EventBus
Parameters:
event - Event to publish.

publishIffNotDead

public void publishIffNotDead(EventCreator creator,
                              java.lang.Class<?>... eventTypes)
Description copied from interface: EventBus
Publishes events iff there is atleast one listener for any of the passed event types. See EventBus javadocs for details about conditional event publishing.
The following is done on receiving this call: This style of publishing must only be used if creating an event object is costly (either due to the size of the object or work required to create such an object) otherwise, the complexity is not worth the effort.

Specified by:
publishIffNotDead in interface EventBus
Parameters:
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.

registerSubscriber

public void registerSubscriber(@Nullable
                               EventFilter filter,
                               java.lang.Object subscriber)
                        throws InvalidSubscriberException
Description copied from interface: EventBus
Registers a subscriber which will be invoked iff the filter passes for the event to be published. This method will attach the filter to all subscriber methods in the passed subscriber. If you need to attach different filters to different methods, then you should register the subscriber without the filter EventBus.registerSubscriber(Object) and then register each filter using EventBus.addFilterForSubscriber(com.netflix.eventbus.spi.EventFilter, com.netflix.eventbus.spi.SubscriberInfo)
See EventBus javadocs for details about subscribers, filters, event-subscriber association and dispatch mode.

Specified by:
registerSubscriber in interface EventBus
Parameters:
filter - Filter.
subscriber - Subscriber to register.
Throws:
InvalidSubscriberException - If the passed subscriber is invalid.

registerSubscriber

public void registerSubscriber(java.lang.Object subscriber)
                        throws InvalidSubscriberException
Description copied from interface: EventBus
Registers a subscriber without any filters. See EventBus javadocs for details about subscribers, filters, event-subscriber association and dispatch mode.

Specified by:
registerSubscriber in interface EventBus
Parameters:
subscriber - Subscriber to register.
Throws:
InvalidSubscriberException - If the passed subscriber is invalid.

enableCatchAllSubscriber

public boolean enableCatchAllSubscriber(java.util.concurrent.BlockingQueue catchAllSink)
Description copied from interface: EventBus
Enables the CatchAllSubscriber for this eventbus with the sink for the subscriber as the passed BlockingQueue instance.

Specified by:
enableCatchAllSubscriber in interface EventBus
Parameters:
catchAllSink - The sink for the CatchAllSubscriber
Returns:
true if the passes sink was successfully attached, iff there is no sink already present.

disableCatchAllSubscriber

public void disableCatchAllSubscriber()
Description copied from interface: EventBus
Disables the CatchAllSubscriber for this eventbus.

Specified by:
disableCatchAllSubscriber in interface EventBus

unregisterSubscriber

public java.util.Set<java.lang.Object> unregisterSubscriber(java.lang.Class<?> subscriberClass)
Description copied from interface: EventBus
Removes the subscriber class (all of its subscriber methods) from the event bus.
If there are more than one instance of the passed class registered with this event bus, all of them are removed. All unconsumed events are rejected.

Specified by:
unregisterSubscriber in interface EventBus
Parameters:
subscriberClass - Subscriber class to unregister.
Returns:
Set of subscriber instances that were removed by this unregister. More than one subscriber instances can be registered with this bus for the same subscriber class. In such a case, there will be more than one instances in the list.

unregisterSubscriber

public boolean unregisterSubscriber(java.lang.Object subscriber)
Description copied from interface: EventBus
Removes the subscriber instance (all of its subscriber methods) from the event bus.
If there are other subscriber instances associated with the class of the passed subscriber, they are left untouched. This should be the preferred mode of unregistration when more than one instances of the same subscriber class are registered and only one is to be removed. All unconsumed events are rejected.

Specified by:
unregisterSubscriber in interface EventBus
Parameters:
subscriber - Subscriber instance to unregister.
Returns:
true if the subscriber instance was unregistered, false otherwise.

addFilterForSubscriber

public void addFilterForSubscriber(EventFilter filter,
                                   SubscriberInfo subscriberInfo)
Description copied from interface: EventBus
Adds the passed filter for the passed subscriber method. See EventBus javadocs for details about subscribers & filters.

Specified by:
addFilterForSubscriber in interface EventBus
Parameters:
filter - Filter to attach.
subscriberInfo - Subscriber info. This can be retrieved by methods EventBus.getAllSubscribersForAnEvent(Class) or EventBus.getAllSubscribers()

removeFiltersForSubscriber

public void removeFiltersForSubscriber(SubscriberInfo subscriberInfo,
                                       EventFilter... filters)
Description copied from interface: EventBus
Removes the passed filters for the passed subscriber. See EventBus javadocs for details about subscribers & filters.

Specified by:
removeFiltersForSubscriber in interface EventBus
Parameters:
subscriberInfo - Subscriber info. This can be retrieved by methods EventBus.getAllSubscribersForAnEvent(Class) or EventBus.getAllSubscribers()
filters - Filters to remove.

clearFiltersForSubscriber

public void clearFiltersForSubscriber(SubscriberInfo subscriberInfo)
Description copied from interface: EventBus
Removes all filters for the passed subscriber method. See EventBus javadocs for details about subscribers & filters.

Specified by:
clearFiltersForSubscriber in interface EventBus
Parameters:
subscriberInfo - Subscriber info. This can be retrieved by methods EventBus.getAllSubscribersForAnEvent(Class) or EventBus.getAllSubscribers()

addFilterForEvent

public void addFilterForEvent(EventFilter filter,
                              java.lang.Class<?> eventClass)
Description copied from interface: EventBus
Adds a publisher level filter for the passed event type. See EventBus javadocs for details about publishers & filters.

Specified by:
addFilterForEvent in interface EventBus
Parameters:
filter - Filter
eventClass - The class of the event for which the filter is to be attached.

removeFiltersForEvent

public void removeFiltersForEvent(java.lang.Class<?> eventClass,
                                  EventFilter... filters)
Description copied from interface: EventBus
Removes the passed filters for the passed event type. See EventBus javadocs for details about publishers & filters.

Specified by:
removeFiltersForEvent in interface EventBus
Parameters:
eventClass - The class of the event for which the filter are to be removed.
filters - Filters to be removed

clearFiltersForEvent

public void clearFiltersForEvent(java.lang.Class<?> eventClass)
Description copied from interface: EventBus
Removes all the filters for the passed event type. See EventBus javadocs for details about publishers & filters.

Specified by:
clearFiltersForEvent in interface EventBus
Parameters:
eventClass - The class of the event for which the filter are to be cleared.

getAllSubscribers

public java.util.Set<SubscriberInfo> getAllSubscribers()
Description copied from interface: EventBus
Returns a set of subscribers that are registered with this event bus.
The details of the filter attached (if any) can be retrieved by the method EventBus.getFilterForASubscriber(com.netflix.eventbus.spi.SubscriberInfo)

Specified by:
getAllSubscribers in interface EventBus
Returns:
An immutable set of currently registered subscribers for the passed event type.

getAllSubscribersForAnEvent

public java.util.Set<SubscriberInfo> getAllSubscribersForAnEvent(java.lang.Class<?> eventType)
Description copied from interface: EventBus
Returns a set of subscribers for that passed event type that are registered with this event bus.
The details of the filter attached (if any) can be retrieved by the method EventBus.getFilterForASubscriber(SubscriberInfo)

Specified by:
getAllSubscribersForAnEvent in interface EventBus
Parameters:
eventType - Event for which the subscribers are to be retrieved.
Returns:
An immutable set of currently registered subscribers.

getFilterForASubscriber

public java.util.Set<EventFilter> getFilterForASubscriber(SubscriberInfo subscriberInfo)
Description copied from interface: EventBus
The set of event filters attached to the passed subscriber.

Specified by:
getFilterForASubscriber in interface EventBus
Parameters:
subscriberInfo - Subscriber info. This can be retrieved by methods EventBus.getAllSubscribersForAnEvent(Class) or EventBus.getAllSubscribers()
Returns:
Immutable set of attached filters. Empty list if none exists.

getFiltersForAnEvent

public java.util.Set<EventFilter> getFiltersForAnEvent(java.lang.Class<?> eventType)
Description copied from interface: EventBus
The set of event filters attached to the passed event type.

Specified by:
getFiltersForAnEvent in interface EventBus
Parameters:
eventType - Event type for which the filters are to be retrieved.
Returns:
Immutable set of attached filters. Empty list if none exists.

getAllRegisteredEventTypes

public java.util.Set<java.lang.Class<?>> getAllRegisteredEventTypes()
Description copied from interface: EventBus
Returns all the event types which have atleast a subscriber or a filter defined in this event bus.

Specified by:
getAllRegisteredEventTypes in interface EventBus
Returns:
Unmodifiable set of all event types registered with this event bus.

shutdown

public void shutdown()