本文主要研究一下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来实现。

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...