Created
August 30, 2018 11:00
-
-
Save bgoetzmann/277a61b1be4bb5bb2731b73884a09f01 to your computer and use it in GitHub Desktop.
A Groovy script for playing with Axon Framework; it takes back the code presented in "A Guide to the Axon Framework", https://www.baeldung.com/axon-cqrs-event-sourcing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See "A Guide to the Axon Framework", https://www.baeldung.com/axon-cqrs-event-sourcing | |
@Grab('org.axonframework:axon-core:3.3.2') | |
import org.axonframework.commandhandling.AggregateAnnotationCommandHandler | |
import org.axonframework.commandhandling.TargetAggregateIdentifier | |
import org.axonframework.commandhandling.model.AggregateIdentifier | |
import org.axonframework.commandhandling.CommandHandler | |
import org.axonframework.eventhandling.EventHandler | |
import org.axonframework.commandhandling.SimpleCommandBus | |
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.inmemory.InMemoryEventStorageEngine | |
import static org.axonframework.commandhandling.model.AggregateLifecycle.apply | |
import groovy.transform.Immutable | |
// Commands | |
@Immutable | |
class CreateMessageCommand { | |
@TargetAggregateIdentifier | |
String id | |
String text | |
} | |
@Immutable | |
class MarkReadMessageCommand { | |
@TargetAggregateIdentifier | |
String id | |
} | |
// Events | |
@Immutable | |
class MessageCreatedEvent { | |
String id | |
String text | |
} | |
@Immutable | |
class MessageReadEvent { | |
String id | |
} | |
// Aggregate | |
class MessagesAggregate { | |
@AggregateIdentifier | |
private String id | |
MessagesAggregate() { | |
} | |
@CommandHandler | |
MessagesAggregate(CreateMessageCommand command) { | |
apply(new MessageCreatedEvent(command.id, command.text)) | |
} | |
@EventHandler | |
void on(MessageCreatedEvent event) { | |
this.id = event.id | |
} | |
@CommandHandler | |
void markRead(MarkReadMessageCommand command) { | |
apply(new MessageReadEvent(id)) | |
} | |
} | |
// Event listener | |
class MessagesEventHandler { | |
@EventHandler | |
void handle(MessageCreatedEvent event) { | |
println "Message received: ${event.text} (${event.id})" | |
} | |
@EventHandler | |
void handle(MessageReadEvent event) { | |
println "Message read: ${event.id}" | |
} | |
} | |
def commandBus = new SimpleCommandBus() | |
def commandGateway = new DefaultCommandGateway(commandBus) | |
def eventStore = new EmbeddedEventStore(new InMemoryEventStorageEngine()) | |
def repository = new EventSourcingRepository(MessagesAggregate, eventStore) | |
def handler = new AggregateAnnotationCommandHandler(MessagesAggregate, repository) | |
handler.subscribe(commandBus) | |
def annotationEventListenerAdapter = new AnnotationEventListenerAdapter(new MessagesEventHandler()) | |
eventStore.subscribe { eventMessages -> | |
eventMessages.each { | |
annotationEventListenerAdapter.handle(it) | |
} | |
} | |
def itemId = UUID.randomUUID().toString() | |
commandGateway.send(new CreateMessageCommand(itemId, 'Hello, how is your day? :-)')) | |
commandGateway.send(new MarkReadMessageCommand(itemId)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment