public abstract class Event { private final UUID id; private final DateTime createdTime; public Event() { this.id = UUID.randomUUID(); this.createdTime = new DateTime(); }}
public final class CustomerArrivedEvent extends Event { private final int customerNumber; public CustomerArrivedEvent(int customerNumber) { super(); this.customerNumber = customerNumber; }}
public class Order { public Order(EventPublisher eventPublisher) { //create order //… eventPublisher.publish(new OrderPlacedEvent()); }}
public class Order { private Listevents; public Order() { //create order //... events.add(new OrderPlacedEvent()); } public List getEvents() { return events; } public void clearEvents() { events.clear(); }}
@Testpublic void shouldPublishEventWhenCreateOrder() { Order order = new Order(); Listevents = order.getEvents(); assertEquals(1, events.size()); Event event = events.get(0); assertTrue(event instanceof OrderPlacedEvent);}
public class OrderRepository { private EventPublisher eventPublisher; public void save(Order order) { //save the order //... Listevents = order.getEvents(); events.forEach(event -> eventPublisher.publish(event)); order.clearEvents(); }}