十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这期内容当中小编将会给大家带来有关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(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实现了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() { ListdomainEvents = 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); 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实现了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的作用是什么了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。