這期內容當中小編將會給大家?guī)碛嘘Pddd-by-examples中DomainEvent的作用是什么,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)是一家專業(yè)從事網(wǎng)站建設、網(wǎng)絡營銷、重慶小程序開發(fā)公司、網(wǎng)站運營為一體的建站企業(yè);在網(wǎng)站建設告別千篇一律,告別似曾相識,這一次我們重新定義網(wǎng)站建設,讓您的網(wǎng)站別具一格。成都響應式網(wǎng)站建設公司,實現(xiàn)全網(wǎng)營銷!一站適應多終端,一樣的建站,不一樣的體驗!
public interface DomainEvent { UUID getEventId(); UUID getAggregateId(); Instant getWhen(); }
DomainEvent接口定義了getEventId、getAggregateId、getWhen方法
public interface DomainEvents { void publish(DomainEvent event); default void publish(Listevents) { 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實現(xiàn)了DomainEvents接口,其publish方法通過spring的ApplicationEventPublisher來發(fā)布
@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實現(xiàn)了DomainEvents接口,其publish先執(zhí)行delegate.publish(event),然后遞增metrics的counter計數(shù)
@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() { ListdomainEvents = eventsStorage.toPublish(); domainEvents.forEach(eventsPublisher::publish); eventsStorage.published(domainEvents); } }
StoreAndForwardDomainEventPublisher實現(xiàn)了DomainEvents接口,其publish方法先通過eventsStorage.save(event)保存事件,之后有一個定時任務不斷執(zhí)行eventsStorage.toPublish()獲取events,發(fā)布事件,最后執(zhí)行eventsStorage.published(domainEvents)
public interface EventsStorage { void save(DomainEvent event); ListtoPublish(); void published(List events); }
EventsStorage接口定義了save、toPublish、published方法
public class InMemoryEventsStorage implements EventsStorage { //it's not thread safe, enough for testing private final java.util.ListeventList = Collections.synchronizedList(new ArrayList<>()); @Override synchronized public void save(DomainEvent event) { eventList.add(event); } @Override synchronized public List toPublish() { return List.ofAll(eventList); } @Override synchronized public void published(List events) { eventList.removeAll(events.asJava()); } }
InMemoryEventsStorage實現(xiàn)了EventsStorage接口,其save方法添加event到eventList,其toPublish方法返回當時的eventList,其published方法從eventList移除指定的events
ddd-by-examples定義了DomainEvent、DomainEvents接口,其中JustForwardDomainEventPublisher、MeteredDomainEventPublisher、StoreAndForwardDomainEventPublisher實現(xiàn)了DomainEvents接口;StoreAndForwardDomainEventPublisher借助了EventsStorage,EventsStorage接口定義了save、toPublish、published方法。
上述就是小編為大家分享的ddd-by-examples中DomainEvent的作用是什么了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。