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 newSimpleDomainEventPublisherwith no subscribers registered.SimpleDomainEventPublisher(List<DomainEventSubscriber<? extends DomainEvent>> subscribers) Creates a newSimpleDomainEventPublisherwith a number of subscribers registered to be notified when events are published. -
Method Summary
Modifier and TypeMethodDescriptionvoidpublish(DomainEvent event) Publishes a domain event, i.e., calls theDomainEventSubscriber.handleEvent(DomainEvent)method for each registered subscriber.voidsubscribe(DomainEventSubscriber<? extends DomainEvent> subscriber) Registers an event handler with this publisher.
-
Constructor Details
-
SimpleDomainEventPublisher
public SimpleDomainEventPublisher()Creates a newSimpleDomainEventPublisherwith no subscribers registered.Use the
subscribe(DomainEventSubscriber)to add subscribers. -
SimpleDomainEventPublisher
Creates a newSimpleDomainEventPublisherwith 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- ifsubscribersisnull
-
-
Method Details
-
publish
Publishes a domain event, i.e., calls theDomainEventSubscriber.handleEvent(DomainEvent)method for each registered subscriber.- Specified by:
publishin interfaceDomainEventPublisher- Parameters:
event- the domain event to publish- Throws:
IllegalArgumentException- ifeventisnull
-
subscribe
Registers an event handler with this publisher.- Specified by:
subscribein interfaceDomainEventPublisher- Parameters:
subscriber- the event handler to register- Throws:
IllegalArgumentException- ifsubscriberisnull
-