2

我正在开发一个小型 cqrs 实现,我对它很陌生。我想将每个处理程序(命令和事件)从聚合中分离出来,并确保所有处理程序都运行良好。命令处理程序从控制器触发,但从那里没有触发事件处理程序。任何人都可以请帮忙。

public class User extends AbstractAnnotatedAggregateRoot<String> {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@AggregateIdentifier
private String userId;
private String userName;
private String age;

public User() {

}

public User(String userid) {
    this.userId=userid;
}

@Override
public String getIdentifier() {
    return this.userId;
}

public void createuserEvent(UserCommand command){
    apply(new UserEvent(command.getUserId()));
}

@EventSourcingHandler
public void applyAccountCreation(UserEvent event) {
    this.userId = event.getUserId();
}

}

public class UserCommand {

private final String userId;

public UserCommand(String userid) {
    this.userId = userid;
}

public String getUserId() {
    return userId;
}

}

@Component 公共类 UserCommandHandler {

@CommandHandler
public void userCreateCommand(UserCommand command) {
    User user = new User(command.getUserId());
    user.createuserEvent(command);
}

}

public class UserEvent {

private final String userId;

public UserEvent(String userid) {
    this.userId = userid;
}

public String getUserId() {
    return userId;
}

}

@Component 公共类 UserEventHandler {

@EventHandler
public void createUser(UserEvent userEvent) {
    System.out.println("Event triggered");
}

}

@Configuration
@AnnotationDriven
public class AppConfiguration {
@Bean
public SimpleCommandBus commandBus() {
    SimpleCommandBus simpleCommandBus = new SimpleCommandBus();
    return simpleCommandBus;
}

@Bean
public Cluster normalCluster() {
    SimpleCluster simpleCluster = new SimpleCluster("simpleCluster");
    return simpleCluster;
}


@Bean
public ClusterSelector clusterSelector() {
    Map<String, Cluster> clusterMap = new HashMap<>();
    clusterMap.put("com.user.event.handler", normalCluster());
    //clusterMap.put("exploringaxon.replay", replayCluster());
    return new ClassNamePrefixClusterSelector(clusterMap);
}



@Bean
public EventBus clusteringEventBus() {
    ClusteringEventBus clusteringEventBus = new ClusteringEventBus(clusterSelector(), terminal());
    return clusteringEventBus;
}


@Bean
public EventBusTerminal terminal() {
    return new EventBusTerminal() {
        @Override
        public void publish(EventMessage... events) {
            normalCluster().publish(events);
        }
        @Override
        public void onClusterCreated(Cluster cluster) {

        }
    };
}

@Bean
public DefaultCommandGateway commandGateway() {
    return new DefaultCommandGateway(commandBus());
}


@Bean
public Repository<User> eventSourcingRepository() {
     EventStore eventStore = new FileSystemEventStore(new SimpleEventFileResolver(new File("D://sevents.txt")));
    EventSourcingRepository eventSourcingRepository = new EventSourcingRepository(User.class, eventStore);
    eventSourcingRepository.setEventBus(clusteringEventBus());
    AnnotationEventListenerAdapter.subscribe(new UserEventHandler(), clusteringEventBus());
    return eventSourcingRepository;
}

}

4

1 回答 1

1

据我所知,唯一缺少的是您没有将User聚合添加到存储库中。通过将其添加到存储库,用户被持久化(通过存储生成的事件,在事件溯源的情况下,或者它的状态),并且命令处理程序(包括聚合)生成的所有事件都发布到事件总线. 请注意,Aggregate 的@EventSourcingHandlers 会立即被调用,但任何外部@EventHandlers 仅在命令处理程序执行后才会调用。

于 2016-10-18T18:24:59.490 回答