2016-10-17 5 views
2

私は小さなcqrsの実装を開発しています。私は非常に新しいです。 私は各ハンドラ(コマンドとイベント)を集合体から分離して、すべて がうまく動作していることを確認したいと思います。コマンドハンドラがコントローラから をトリガしていますが、そこからイベントハンドラはトリガされません。 誰でもこれを手伝ってください。Axonイベントハンドラが機能しない

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; 
} 

}

+0

コードを編集してください。 – surajsn

答えて

1

私の知る限り、唯一欠けているものは、あなたが上がらないということですリポジトリにUser集計を追加していません。これをリポジトリに追加することで、ユーザーは永続化されます(生成されたイベント、Event Sourcingの場合はその状態、その他の状態は保存されます)、コマンドハンドラ(集合体を含む)によって生成されたすべてのイベントはイベントバス。 集約の@EventSourcingHandlerはすぐに呼び出されますが、外部の@EventHandlerは、コマンドハンドラが実行された後にのみ呼び出されることに注意してください。

+0

非常にありがとうございます – Prasanth

関連する問題