BAEL-2435 Update main application

Update the MessagesRunner to be a spring boot application
This commit is contained in:
Steven van Beelen 2018-12-18 15:17:16 +01:00
parent d8ef050867
commit 557ce65552
1 changed files with 5 additions and 46 deletions

View File

@ -1,54 +1,13 @@
package com.baeldung.axon;
import com.baeldung.axon.aggregates.MessagesAggregate;
import com.baeldung.axon.commands.CreateMessageCommand;
import com.baeldung.axon.commands.MarkReadMessageCommand;
import com.baeldung.axon.eventhandlers.MessagesEventHandler;
import org.axonframework.commandhandling.AggregateAnnotationCommandHandler;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.SimpleCommandBus;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.commandhandling.gateway.DefaultCommandGateway;
import org.axonframework.eventhandling.AnnotationEventListenerAdapter;
import org.axonframework.eventsourcing.EventSourcingRepository;
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore;
import org.axonframework.eventsourcing.eventstore.EventStore;
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
import java.util.UUID;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MessagesRunner {
public static void main(String[] args) {
CommandBus commandBus = new SimpleCommandBus();
CommandGateway commandGateway = new DefaultCommandGateway(commandBus);
EventStore eventStore = new EmbeddedEventStore(new InMemoryEventStorageEngine());
EventSourcingRepository<MessagesAggregate> repository =
new EventSourcingRepository<>(MessagesAggregate.class, eventStore);
AggregateAnnotationCommandHandler<MessagesAggregate> messagesAggregateAggregateAnnotationCommandHandler =
new AggregateAnnotationCommandHandler<MessagesAggregate>(MessagesAggregate.class, repository);
messagesAggregateAggregateAnnotationCommandHandler.subscribe(commandBus);
final AnnotationEventListenerAdapter annotationEventListenerAdapter =
new AnnotationEventListenerAdapter(new MessagesEventHandler());
eventStore.subscribe(eventMessages -> eventMessages.forEach(e -> {
try {
annotationEventListenerAdapter.handle(e);
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
));
final String itemId = UUID.randomUUID().toString();
commandGateway.send(new CreateMessageCommand(itemId, "Hello, how is your day? :-)"));
commandGateway.send(new MarkReadMessageCommand(itemId));
SpringApplication.run(MessagesRunner.class, args);
}
}