Class ThreadLocalDomainEventPublisher

java.lang.Object
com.reallifedeveloper.common.domain.event.ThreadLocalDomainEventPublisher
All Implemented Interfaces:
DomainEventPublisher

public class ThreadLocalDomainEventPublisher extends Object implements DomainEventPublisher
A publisher of domain events that keeps track of subscribers on a per-thread basis. It is assumed that subscription and publishing are done by the same thread, and 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.

If threads are reused, it is important to call the reset() method to clear any previous subscribers.

The normal use-case for this class is as follows:

  • A request comes in to an application service.
  • The application service creates or retrieves an instance of this class and calls the reset() method.
  • The application service registers all necessary subscribers using the subscribe(DomainEventSubscriber) method.
  • The application service delegates to domain services or aggregates, which publish events when something interesting happens in the domain, using the publish(DomainEvent) method.
Author:
RealLifeDeveloper
  • Constructor Details

    • ThreadLocalDomainEventPublisher

      public ThreadLocalDomainEventPublisher()
  • Method Details