Class SimpleDomainEventPublisher
java.lang.Object
com.reallifedeveloper.common.domain.event.SimpleDomainEventPublisher
- All Implemented Interfaces:
DomainEventPublisher
A publisher of domain events that holds subscribers in a list. Publishing is handled
synchronously; to handle events asynchronously, a subscriber could send a message to
a message queue or store the event for later processing.
The idea behind this publisher is that it will be configured once and for all, for example using Spring:
<bean class="th.co.edge.domain.event.SimpleDomainEventPublisher"> <constructor-arg> <list> <bean class="com.foo.FooSubscriber" /> <bean class="com.foo.BarSubscriber" /> </list> </constructor-arg> </bean>
The publisher can then be injected in the classes that need to publish events:
@Autowired private DomainEventSubscriber eventSubscriber;
Used this way in a normal enterprise application, several threads may call the
publish(DomainEvent)
method simultaneously, so the subscribers should
be thread safe.
- Author:
- RealLifeDeveloper
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newSimpleDomainEventPublisher
with no subscribers registered.SimpleDomainEventPublisher
(List<DomainEventSubscriber<? extends DomainEvent>> subscribers) Creates a newSimpleDomainEventPublisher
with a number of subscribers registered to be notified when events are published. -
Method Summary
Modifier and TypeMethodDescriptionvoid
publish
(DomainEvent event) Publishes a domain event, i.e., calls theDomainEventSubscriber.handleEvent(DomainEvent)
method for each registered subscriber.void
subscribe
(DomainEventSubscriber<? extends DomainEvent> subscriber) Registers an event handler with this publisher.
-
Constructor Details
-
SimpleDomainEventPublisher
public SimpleDomainEventPublisher()Creates a newSimpleDomainEventPublisher
with no subscribers registered.Use the
subscribe(DomainEventSubscriber)
to add subscribers. -
SimpleDomainEventPublisher
Creates a newSimpleDomainEventPublisher
with a number of subscribers registered to be notified when events are published.- Parameters:
subscribers
- a list of subscribers to notify when events are published- Throws:
IllegalArgumentException
- ifsubscribers
isnull
-
-
Method Details
-
publish
Publishes a domain event, i.e., calls theDomainEventSubscriber.handleEvent(DomainEvent)
method for each registered subscriber.- Specified by:
publish
in interfaceDomainEventPublisher
- Parameters:
event
- the domain event to publish- Throws:
IllegalArgumentException
- ifevent
isnull
-
subscribe
Registers an event handler with this publisher.- Specified by:
subscribe
in interfaceDomainEventPublisher
- Parameters:
subscriber
- the event handler to register- Throws:
IllegalArgumentException
- ifsubscriber
isnull
-