Package org.apache.shiro.event.support
Class DefaultEventBus
- java.lang.Object
-
- org.apache.shiro.event.support.DefaultEventBus
-
- All Implemented Interfaces:
EventBus
public class DefaultEventBus extends Object implements EventBus
A default event bus implementation that synchronously publishes events to registered listeners. Listeners can be registered or unregistered for events as necessary. An event bus enables a publish/subscribe paradigm within Shiro - components can publish or consume events they find relevant without needing to be tightly coupled to other components. This affords great flexibility within Shiro by promoting loose coupling and high cohesion between components and a much safer pluggable architecture that is more resilient to change over time.Sending Events
If a component wishes to publish events to other components:MyEvent myEvent = createMyEvent(); eventBus.publish(myEvent);
The event bus will determine the type of event and then dispatch the event to components that wish to receive events of that type.Receiving Events
A component can receive events of interest by doing the following.- For each type of event you wish to consume, create a public method that accepts a single event argument. The method argument type indicates the type of event to receive.
- Annotate each of these public methods with the
Subscribe
annotation. - Register the component with the event bus:
eventBus.register(myComponent);
Subscribe
-annotated method(s) will be invoked as expected. This design (and its constituent helper components) was largely influenced by Guava's EventBus concept, although no code was shared/imported (even though Guava is Apache 2.0 licensed and could have been used). This implementation is thread-safe and may be used concurrently.- Since:
- 1.3
-
-
Constructor Summary
Constructors Constructor Description DefaultEventBus()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EventListenerResolver
getEventListenerResolver()
void
publish(Object event)
Publishes the specified event to an event subsystem that will deliver events to relevantSubscribe
rs.void
register(Object instance)
Registers all event handler methods on the specified instance to receive relevant events.void
setEventListenerResolver(EventListenerResolver eventListenerResolver)
void
unregister(Object instance)
Unregisters all previously-registered event handler methods on the specified instance.
-
-
-
Constructor Detail
-
DefaultEventBus
public DefaultEventBus()
-
-
Method Detail
-
getEventListenerResolver
public EventListenerResolver getEventListenerResolver()
-
setEventListenerResolver
public void setEventListenerResolver(EventListenerResolver eventListenerResolver)
-
publish
public void publish(Object event)
Description copied from interface:EventBus
Publishes the specified event to an event subsystem that will deliver events to relevantSubscribe
rs.
-
register
public void register(Object instance)
Description copied from interface:EventBus
Registers all event handler methods on the specified instance to receive relevant events. The handler methods are determined by theEventBus
implementation, typically by using anEventListenerResolver
(e.g.AnnotationEventListenerResolver
).
-
unregister
public void unregister(Object instance)
Description copied from interface:EventBus
Unregisters all previously-registered event handler methods on the specified instance. If the specified object was not previously registered, calling this method has no effect.- Specified by:
unregister
in interfaceEventBus
- Parameters:
instance
- the previously
-
-