聊聊cheddar的DomainEvent
序
本文主要研究一下cheddar的DomainEvent
DomainEvent
Cheddar/cheddar/cheddar-domain/src/main/java/com/clicktravel/cheddar/domain/event/DomainEvent.java
public interface DomainEvent extends Event {
}
DomainEvent接口继承了Event接口
AbstractDomainEvent
Cheddar/cheddar/cheddar-domain/src/main/java/com/clicktravel/cheddar/domain/event/AbstractDomainEvent.java
public abstract class AbstractDomainEvent extends AbstractEvent implements DomainEvent {
public abstract String context();
@Override
public final String type() {
return context() + "." + getClass().getSimpleName();
}
}
AbstractDomainEvent继承了AbstractEvent,声明实现了DomainEvent接口,它声明了一个抽象的context方法
DomainEventHandler
Cheddar/cheddar/cheddar-domain/src/main/java/com/clicktravel/cheddar/domain/event/DomainEventHandler.java
public interface DomainEventHandler extends EventHandler<DomainEvent> {
}
public interface HighPriorityDomainEventHandler extends DomainEventHandler {
}
public interface LowPriorityDomainEventHandler extends DomainEventHandler {
}
DomainEventHandler接口继承了EventHandler接口,其泛型为DomainEvent;HighPriorityDomainEventHandler及LowPriorityDomainEventHandler接口继承了DomainEventHandler接口
DomainEventPublisher
Cheddar/cheddar/cheddar-domain/src/main/java/com/clicktravel/cheddar/domain/event/DomainEventPublisher.java
public class DomainEventPublisher extends EventPublisher<DomainEvent> {
private static DomainEventPublisher instance;
public static void init(final MessagePublisher<TypedMessage> messagePublisher) {
instance = new DomainEventPublisher(messagePublisher);
}
private DomainEventPublisher(final MessagePublisher<TypedMessage> messagePublisher) {
super(messagePublisher);
}
public static DomainEventPublisher instance() {
if (instance == null) {
throw new IllegalStateException("DomainEventPublisher not initialized");
}
return instance;
}
}
DomainEventPublisher继承了EventPublisher,其构造器接收MessagePublisher;它提供了init方法用于创建DomainEventPublisher,instance方法用于获取instance
小结
cheddar定义了DomainEvent接口及AbstractDomainEvent抽象类;DomainEventHandler接口继承了EventHandler接口,其泛型为DomainEvent;DomainEventPublisher继承了EventPublisher,其构造器接收MessagePublisher,其publishEvent方法最后通过MessagePublisher的publish来实现。