Skip to content

Instantly share code, notes, and snippets.

@lc-nyovchev
Last active October 12, 2016 14:01
Show Gist options
  • Save lc-nyovchev/fc5d8c373a4f21770f9c5b4b2f2870b3 to your computer and use it in GitHub Desktop.
Save lc-nyovchev/fc5d8c373a4f21770f9c5b4b2f2870b3 to your computer and use it in GitHub Desktop.
@Configuration
@Import(CassandraTestConfig.class)
public class AxonTestContext {
@Bean
public EventStorageEngine eventStorageEngine(Cluster cluster){
return new CassandraEventStorageEngine(cluster, new XStreamSerializer());
}
@Bean
public EventStore eventStore(EventStorageEngine eventStorageEngine){
return new AbstractEventStore(eventStorageEngine) {
@Override
public TrackingEventStream streamEvents(TrackingToken trackingToken) {
return null;
}
};
}
@Bean
public EventSourcingRepository<TestAggregate> aggregateEventSourcingRepository(EventStore eventStore){
return new EventSourcingRepository<>(TestAggregate.class, eventStore);
}
@Bean
public CommandBus commandBus(){
CommandBus commandBus = new AsynchronousCommandBus();
AnnotationCommandHandlerAdapter annotationCommandHandlerAdapter = new AnnotationCommandHandlerAdapter(
new TestAggregate()
);
commandBus.subscribe(TestAggregateCreateCommand.class.getName(), annotationCommandHandlerAdapter);
commandBus.subscribe(TestAggregateNameChangeCommand.class.getName(), annotationCommandHandlerAdapter);
return commandBus;
}
}
@NoArgsConstructor
public class TestAggregate {
@AggregateIdentifier private UUID someAggregateId;
private String name;
@CommandHandler
public TestAggregate(TestAggregateCreateCommand testAggregateCreateCommand){
apply(new TestAggregateCreatedEvent(testAggregateCreateCommand.getTestAggregateId()));
}
@CommandHandler
public void setName(TestAggregateNameChangeCommand testAggregateNameChangeCommand){
apply(new TestAggregateNameChangedEvent(
testAggregateNameChangeCommand.getTestAggregateId(),
testAggregateNameChangeCommand.getName()
));
}
@EventSourcingHandler
public void on(TestAggregateCreatedEvent createdEvent){
this.someAggregateId = createdEvent.getSomeAggregateId();
}
@EventSourcingHandler
public void on(TestAggregateNameChangedEvent testAggregateNameChangedEvent){
this.name = testAggregateNameChangedEvent.getName();
}
}
@abuijze
Copy link

abuijze commented Oct 12, 2016

Check out the method: AnnotationCommandHandlerAdapter.subscribe(). It will automatically subscribe itself as the handler for all known Command types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment