2016-10-17 1 views
2

Je développe une petite implémentation de cqrs et je suis très novice en la matière. Je souhaite séparer chaque gestionnaire (commande et événement) de l'agrégat et pour m'assurer que tout fonctionne correctement. Le gestionnaire de commande est déclenché à partir du contrôleur mais à partir de là les gestionnaires d'événements ne sont pas déclenchés. Pourrait personne S'il vous plaît aider à ce sujet.Le gestionnaire d'événements Axon ne fonctionne pas

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 public class {

@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 public class {

@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

Veuillez éditer votre code. – surajsn

Répondre

1

Pour autant que je peux dire, la seule chose qui manque est que vous aren ne pas ajouter le User Agréger à un référentiel. En l'ajoutant au référentiel, l'utilisateur est conservé (soit en stockant les événements générés, dans le cas d'un événement, soit son état) et tous les événements générés par le gestionnaire de commandes (y compris l'agrégat) sont publiés dans le bus d'événements. . Notez que les @EventSourcingHandler s de l'agrégat sont appelés immédiatement, mais que les @EventHandler externes ne sont invoqués qu'après l'exécution du gestionnaire de commandes.

+0

Merci beaucoup Allard – Prasanth