Permalink
Browse files

added the first part of a axon-vertx sample

  • Loading branch information...
jettro committed Nov 10, 2012
1 parent d5c3541 commit 86ad19c6a926f8c61077dc575284d6e3a13bce46
@@ -0,0 +1,12 @@
+import org.vertx.groovy.core.eventbus.EventBus
+
+def log = container.logger
+
+EventBus eventBus = vertx.eventBus
+
+container.with {
+ deployModule('todo') {
+ log.info "App: The Todo module is deployed."
+ eventBus.publish("command.todo.create",["todoText":"This is a new TODO"])
+ }
+}
@@ -0,0 +1,5 @@
+log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.Stdout.layout.conversionPattern=%d [%t] %-5p %x - %m%n
+
+log4j.rootLogger=DEBUG,Stdout
@@ -0,0 +1,62 @@
+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.commandhandling.CommandHandler
+import org.axonframework.commandhandling.CommandMessage
+import org.axonframework.unitofwork.UnitOfWork
+import org.vertx.groovy.core.eventbus.EventBus
+import commands.CreateTodoCommand
+import aggregates.Todo
+import org.axonframework.eventhandling.SimpleEventBus
+import org.vertx.groovy.core.eventbus.EventBus
+import org.axonframework.eventsourcing.EventSourcingRepository
+import org.axonframework.eventstore.mongo.MongoEventStore
+import org.axonframework.eventstore.mongo.MongoTemplate
+import org.axonframework.eventstore.mongo.DefaultMongoTemplate
+import com.mongodb.Mongo
+import org.axonframework.domain.EventMessage
+import events.TodoCreatedEvent
+
+def log = container.logger
+
+log.info "The Todo mod is started."
+
+CommandBus commandBus = new SimpleCommandBus()
+commandBus.subscribe("commands.CreateTodoCommand", new CommandHandler(){
+
+ Object handle(CommandMessage commandMessage, UnitOfWork unitOfWork) {
+ CreateTodoCommand command = commandMessage.payload as CreateTodoCommand
+ log.info "The received command is ${command.todoText}"
+ new Todo(command.identifier,command.todoText)
+ return null
+ }
+})
+
+org.axonframework.eventhandling.EventBus axonEventBus = new SimpleEventBus()
+axonEventBus.subscribe(new org.axonframework.eventhandling.EventListenerProxy(){
+
+ void handle(EventMessage eventMessage) {
+ println "We received an event ${eventMessage.getPayload().todoText}"
+ }
+
+ Class<?> getTargetType() {
+ return TodoCreatedEvent.class
+ }
+})
+
+def todoRepository = new EventSourcingRepository<Todo>(Todo.class) {}
+MongoTemplate mongoTemplate = new DefaultMongoTemplate(new Mongo())
+MongoEventStore eventStore = new MongoEventStore(mongoTemplate)
+todoRepository.setEventStore(eventStore)
+todoRepository.setEventBus(axonEventBus)
+
+CommandGateway gateway = new DefaultCommandGateway(commandBus)
+
+
+// VERTX
+EventBus eventBus = vertx.eventBus
+
+eventBus.registerHandler("command.todo.create") {message ->
+ gateway.send(new CreateTodoCommand("id-1", message.body.todoText))
+}
@@ -0,0 +1,37 @@
+package aggregates
+
+import org.axonframework.domain.DomainEventMessage
+import org.axonframework.eventsourcing.AbstractEventSourcedAggregateRoot
+import org.axonframework.eventsourcing.EventSourcedEntity
+import events.TodoCreatedEvent
+
+/**
+ * @author Jettro Coenradie
+ */
+class Todo extends AbstractEventSourcedAggregateRoot {
+ TodoIdentifier aggregateIdentifier
+
+ Todo() {
+
+ }
+
+ Todo(String identifier, String todoText) {
+ println "Trying to create a new ToDo item with id ${identifier}"
+ apply(new TodoCreatedEvent(new TodoIdentifier(identifier), todoText))
+ }
+
+ @Override
+ protected Iterable<? extends EventSourcedEntity> getChildEntities() {
+ return null
+ }
+
+ @Override
+ protected void handle(DomainEventMessage domainEventMessage) {
+ this.aggregateIdentifier = ((TodoCreatedEvent) domainEventMessage.getPayload()).getIdentifier();
+ println "Handle a new event ${((TodoCreatedEvent)domainEventMessage.getPayload()).todoText}"
+ }
+
+ Object getIdentifier() {
+ return aggregateIdentifier
+ }
+}
@@ -0,0 +1,34 @@
+package aggregates
+
+import org.axonframework.domain.IdentifierFactory
+import org.axonframework.common.Assert
+/**
+ * @author Jettro Coenradie
+ */
+class TodoIdentifier implements Serializable {
+ String identifier
+
+ TodoIdentifier() {
+ this.identifier = IdentifierFactory.getInstance().generateIdentifier();
+ }
+
+ TodoIdentifier(String identifier) {
+ Assert.notNull(identifier, "Identifier may not be null");
+ this.identifier = identifier
+ }
+
+ boolean equals(o) {
+ if (this.is(o)) return true
+ if (getClass() != o.class) return false
+
+ TodoIdentifier that = (TodoIdentifier) o
+
+ if (identifier != that.identifier) return false
+
+ return true
+ }
+
+ int hashCode() {
+ return (identifier != null ? identifier.hashCode() : 0)
+ }
+}
@@ -0,0 +1,14 @@
+package commands
+
+/**
+ * @author Jettro Coenradie
+ */
+class CreateTodoCommand {
+ String identifier
+ String todoText
+
+ CreateTodoCommand(String identifier, String todoText) {
+ this.identifier = identifier
+ this.todoText = todoText
+ }
+}
@@ -0,0 +1,20 @@
+package events
+
+import aggregates.TodoIdentifier
+
+/**
+ * @author Jettro Coenradie
+ */
+class TodoCreatedEvent {
+ TodoIdentifier aggregateIdentifier;
+ String todoText
+
+ TodoCreatedEvent(TodoIdentifier identifier, String todoText) {
+ this.aggregateIdentifier = identifier
+ this.todoText = todoText
+ }
+
+ TodoIdentifier getIdentifier() {
+ return aggregateIdentifier
+ }
+}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
+{
+ "main":"Interface.groovy"
+}

0 comments on commit 86ad19c

Please sign in to comment.