Skip to content

Commit

Permalink
added the first part of a axon-vertx sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jettro committed Nov 10, 2012
1 parent d5c3541 commit 86ad19c
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 0 deletions.
12 changes: 12 additions & 0 deletions axon-todo-list/app/App.groovy
@@ -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"])
}
}
5 changes: 5 additions & 0 deletions axon-todo-list/log4j.properties
@@ -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
62 changes: 62 additions & 0 deletions axon-todo-list/mods/todo/Interface.groovy
@@ -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))
}
37 changes: 37 additions & 0 deletions axon-todo-list/mods/todo/aggregates/Todo.groovy
@@ -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
}
}
34 changes: 34 additions & 0 deletions axon-todo-list/mods/todo/aggregates/TodoIdentifier.groovy
@@ -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)
}
}
14 changes: 14 additions & 0 deletions axon-todo-list/mods/todo/commands/CreateTodoCommand.groovy
@@ -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
}
}
20 changes: 20 additions & 0 deletions axon-todo-list/mods/todo/events/TodoCreatedEvent.groovy
@@ -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 added axon-todo-list/mods/todo/lib/dom4j-1.6.1.jar
Binary file not shown.
Binary file not shown.
Binary file added axon-todo-list/mods/todo/lib/slf4j-api-1.6.6.jar
Binary file not shown.
Binary file not shown.
Binary file added axon-todo-list/mods/todo/lib/xmlpull-1.1.3.1.jar
Binary file not shown.
Binary file added axon-todo-list/mods/todo/lib/xpp3_min-1.1.4c.jar
Binary file not shown.
Binary file added axon-todo-list/mods/todo/lib/xstream-1.4.3.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions axon-todo-list/mods/todo/mod.json
@@ -0,0 +1,3 @@
{
"main":"Interface.groovy"
}

0 comments on commit 86ad19c

Please sign in to comment.