这期内容当中小编将会给大家带来有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。