I am trying to wrap my head around Axon with Spring Boot, Groovy, AMQP and MongoDb.
I am trying to recreate and enhance the TODOItem app and what i found out so far is that i need a EventSourcngRepository for every RootAgregate i have in my app.
Why is that? I have only one eventstore which every repository is referenced to, so why cant i have a default generic Repository that will handle all Aggregate creations.
As long as the aggregates have a noArgs constructor (which is required with event sourcing) this shgould not be a problem imo.
Could somebody clarify this for me?
My current config looks like this, maybe i can even strip it down:
@Configuration
@AnnotationDriven
public class AxonConfiguration {
@Autowired
Environment env
@Bean
FanoutExchange eventBusExchange() {
new FanoutExchange(
env.getRequiredProperty("spring.rabbitmq.axon.exchangeName"),
env.getRequiredProperty("spring.rabbitmq.axon.exchangeDurable", Boolean),
env.getRequiredProperty("spring.rabbitmq.axon.exchangeAutoDelete", Boolean)
)
}
// Event bus queue
@Bean
public Queue eventBusQueue() {
new Queue(
env.getRequiredProperty("spring.rabbitmq.axon.queueName"),
env.getRequiredProperty("spring.rabbitmq.axon.queueDurable", Boolean),
env.getRequiredProperty("spring.rabbitmq.axon.queueExclusive", Boolean),
env.getRequiredProperty("spring.rabbitmq.axon.queueAutoDelete", Boolean)
)
}
@Bean
public Binding binding() {
BindingBuilder.bind(eventBusQueue()).to(eventBusExchange())
}
@Bean
CommandBus commandBus() {
new SimpleCommandBus()
}
@Bean
public XStreamSerializer xstreamSerializer() {
new XStreamSerializer()
}
@Bean
public EventBusTerminal terminal(ConnectionFactory factory, XStreamSerializer serializer, ListenerContainerLifecycleManager lifecycleManager) {
def terminal = new SpringAMQPTerminal()
terminal.setConnectionFactory(factory)
terminal.setSerializer(serializer)
terminal.setExchangeName(env.getRequiredProperty("spring.rabbitmq.axon.exchangeName"))
terminal.setListenerContainerLifecycleManager(lifecycleManager)
terminal.setDurable(false)
terminal.setTransactional(false)
return terminal
}
@Bean
AMQPConsumerConfiguration springAMQPConsumerConfiguration(ConnectionFactory factory) {
def springAMQPConsumerConfiguration = new SpringAMQPConsumerConfiguration()
springAMQPConsumerConfiguration.setDefaults(null)
springAMQPConsumerConfiguration.setQueueName(env.getRequiredProperty("spring.rabbitmq.axon.queueName"))
springAMQPConsumerConfiguration.setErrorHandler(TaskUtils.getDefaultErrorHandler(false))
springAMQPConsumerConfiguration.setAcknowledgeMode(AcknowledgeMode.AUTO)
springAMQPConsumerConfiguration.setConcurrentConsumers(1)
springAMQPConsumerConfiguration.setRecoveryInterval(env.getRequiredProperty("spring.rabbitmq.axon.recoveryInterval", Long))
springAMQPConsumerConfiguration.setExclusive(false)
springAMQPConsumerConfiguration.setPrefetchCount(env.getRequiredProperty("spring.rabbitmq.axon.prefetchCount", Integer))
springAMQPConsumerConfiguration.setTransactionManager(new RabbitTransactionManager(factory))
springAMQPConsumerConfiguration.setTxSize(env.getRequiredProperty("spring.rabbitmq.axon.transactionSize", Integer))
return springAMQPConsumerConfiguration
}
@Bean
SimpleCluster simpleCluster() {
new SimpleCluster(env.getRequiredProperty("spring.rabbitmq.axon.queueName"))
}
@Bean
DefaultAMQPMessageConverter defaultAMQPMessageConverter(XStreamSerializer serializer) {
new DefaultAMQPMessageConverter(serializer)
}
@Bean
ListenerContainerLifecycleManager listenerContainerLifecycleManager(ConnectionFactory factory) {
def listenerContainerLifecycleManager = new ListenerContainerLifecycleManager()
listenerContainerLifecycleManager.setConnectionFactory(factory)
return listenerContainerLifecycleManager
}
@Bean
public CommandGateway commandGateway(CommandBus commandBus) {
new DefaultCommandGateway(commandBus)
}
@Bean
EventBus eventBus() {
def bus = new SimpleEventBus()
return bus
}
@Bean
MongoTemplate axonMongoTemplate(MongoClient client) {
new DefaultMongoTemplate(
client
)
}
@Bean
EventStore eventStore(MongoTemplate template) {
new MongoEventStore(
template
)
}
@Bean
public EventSourcingRepository<ToDoItem> eventSourcingRepository(EventStore eventStore, EventBus eventBus) {
EventSourcingRepository<ToDoItem> repository = new EventSourcingRepository(ToDoItem, eventStore);
repository.setEventBus(eventBus);
return repository;
}
}