您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關ddd-by-examples中DomainEvent的作用是什么,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
public interface DomainEvent { UUID getEventId(); UUID getAggregateId(); Instant getWhen(); }
DomainEvent接口定義了getEventId、getAggregateId、getWhen方法
public interface DomainEvents { void publish(DomainEvent event); default void publish(List<DomainEvent> events) { events.forEach(this::publish); } }
DomainEvents接口定義了publish方法,同時提供一個批量的publish方法
@AllArgsConstructor public class JustForwardDomainEventPublisher implements DomainEvents { private final ApplicationEventPublisher applicationEventPublisher; @Override public void publish(DomainEvent event) { applicationEventPublisher.publishEvent(event); } }
JustForwardDomainEventPublisher實現了DomainEvents接口,其publish方法通過spring的ApplicationEventPublisher來發布
@AllArgsConstructor public class MeteredDomainEventPublisher implements DomainEvents { private static final String DOMAIN_EVENTS = "domain_events"; private static final String TAG_NAME = "name"; private final DomainEvents delegate; private final MeterRegistry metricsRegistry; @Override public void publish(DomainEvent event) { delegate.publish(event); metricsRegistry.counter(DOMAIN_EVENTS, TAG_NAME, event.getClass().getSimpleName()).increment(); } }
MeteredDomainEventPublisher實現了DomainEvents接口,其publish先執行delegate.publish(event),然后遞增metrics的counter計數
@AllArgsConstructor public class StoreAndForwardDomainEventPublisher implements DomainEvents { private final DomainEvents eventsPublisher; private final EventsStorage eventsStorage; @Override public void publish(DomainEvent event) { eventsStorage.save(event); } @Scheduled(fixedRate = 3000L) @Transactional public void publishAllPeriodically() { List<DomainEvent> domainEvents = eventsStorage.toPublish(); domainEvents.forEach(eventsPublisher::publish); eventsStorage.published(domainEvents); } }
StoreAndForwardDomainEventPublisher實現了DomainEvents接口,其publish方法先通過eventsStorage.save(event)保存事件,之后有一個定時任務不斷執行eventsStorage.toPublish()獲取events,發布事件,最后執行eventsStorage.published(domainEvents)
public interface EventsStorage { void save(DomainEvent event); List<DomainEvent> toPublish(); void published(List<DomainEvent> events); }
EventsStorage接口定義了save、toPublish、published方法
public class InMemoryEventsStorage implements EventsStorage { //it's not thread safe, enough for testing private final java.util.List<DomainEvent> eventList = Collections.synchronizedList(new ArrayList<>()); @Override synchronized public void save(DomainEvent event) { eventList.add(event); } @Override synchronized public List<DomainEvent> toPublish() { return List.ofAll(eventList); } @Override synchronized public void published(List<DomainEvent> events) { eventList.removeAll(events.asJava()); } }
InMemoryEventsStorage實現了EventsStorage接口,其save方法添加event到eventList,其toPublish方法返回當時的eventList,其published方法從eventList移除指定的events
ddd-by-examples定義了DomainEvent、DomainEvents接口,其中JustForwardDomainEventPublisher、MeteredDomainEventPublisher、StoreAndForwardDomainEventPublisher實現了DomainEvents接口;StoreAndForwardDomainEventPublisher借助了EventsStorage,EventsStorage接口定義了save、toPublish、published方法。
上述就是小編為大家分享的ddd-by-examples中DomainEvent的作用是什么了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。