Permalink
Browse files

About to insert multiple files to for the axon-vertx sample

  • Loading branch information...
jettro committed Nov 11, 2012
1 parent 86ad19c commit c9b117b411b6c98c05d9a31f78232cd80ea10cdf
@@ -5,6 +5,9 @@ def log = container.logger
EventBus eventBus = vertx.eventBus
container.with {
+ deployModule('web') {
+ log.info "App: The web module is deployed."
+ }
deployModule('todo') {
log.info "App: The Todo module is deployed."
eventBus.publish("command.todo.create",["todoText":"This is a new TODO"])
@@ -2,4 +2,4 @@ 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
+log4j.rootLogger=INFO,Stdout
@@ -1,62 +1,58 @@
+import aggregates.Todo
+import aggregates.TodoCommandHandler
+import aggregates.TodoIdentifier
+import com.mongodb.Mongo
+import com.thoughtworks.xstream.XStream
+import commands.CreateTodoCommand
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.DefaultMongoTemplate
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
+import org.axonframework.serializer.bson.DBObjectXStreamSerializer
+import org.vertx.groovy.core.eventbus.EventBus
+import query.TodoEventListener
def log = container.logger
log.info "The Todo mod is started."
+/* Axon infrastructure configuration */
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
- }
-})
-
+CommandGateway gateway = new DefaultCommandGateway(commandBus)
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}"
- }
+def xstream = new XStream()
+xstream.classLoader = getClass().classLoader
+def serializer = new DBObjectXStreamSerializer(xstream)
- Class<?> getTargetType() {
- return TodoCreatedEvent.class
- }
-})
+MongoTemplate mongoTemplate = new DefaultMongoTemplate(new Mongo(), "axon-todo", "domain", "saga", null, null)
+MongoEventStore eventStore = new MongoEventStore(serializer, mongoTemplate)
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)
+/* Register the command handlers with the command bus */
+def handler = new TodoCommandHandler(todoRepository, container)
+commandBus.subscribe("commands.CreateTodoCommand", handler)
+commandBus.subscribe("commands.MarkTodoAsCompleteCommand", handler)
-
-// VERTX
+/* VERTX Configuration */
EventBus eventBus = vertx.eventBus
+/* Register the listeners with the event bus for the query database */
+axonEventBus.subscribe(new TodoEventListener(eventBus, container))
+
+
eventBus.registerHandler("command.todo.create") {message ->
- gateway.send(new CreateTodoCommand("id-1", message.body.todoText))
+ message.reply(["message": "We have received a new ToDo"])
+ def identifier = new TodoIdentifier()
+ gateway.send(new CreateTodoCommand(identifier, message.body.todoText))
+
+// gateway.send(new MarkTodoAsCompleteCommand(identifier))
}
@@ -4,20 +4,25 @@ import org.axonframework.domain.DomainEventMessage
import org.axonframework.eventsourcing.AbstractEventSourcedAggregateRoot
import org.axonframework.eventsourcing.EventSourcedEntity
import events.TodoCreatedEvent
+import events.TodoMarkedAsCompleteEvent
/**
+ * Aggregate root for todoItems
+ *
* @author Jettro Coenradie
*/
class Todo extends AbstractEventSourcedAggregateRoot {
TodoIdentifier aggregateIdentifier
+ TodoStatus status
- Todo() {
+ Todo() {}
+ Todo(TodoIdentifier identifier, String todoText) {
+ apply(new TodoCreatedEvent(identifier, todoText))
}
- Todo(String identifier, String todoText) {
- println "Trying to create a new ToDo item with id ${identifier}"
- apply(new TodoCreatedEvent(new TodoIdentifier(identifier), todoText))
+ void markAsComplete() {
+ apply(new TodoMarkedAsCompleteEvent(aggregateIdentifier))
}
@Override
@@ -27,11 +32,25 @@ class Todo extends AbstractEventSourcedAggregateRoot {
@Override
protected void handle(DomainEventMessage domainEventMessage) {
- this.aggregateIdentifier = ((TodoCreatedEvent) domainEventMessage.getPayload()).getIdentifier();
- println "Handle a new event ${((TodoCreatedEvent)domainEventMessage.getPayload()).todoText}"
+ switch (domainEventMessage.payloadType) {
+ case TodoCreatedEvent.class:
+ this.aggregateIdentifier = (domainEventMessage.payload as TodoCreatedEvent).getIdentifier();
+ this.status = TodoStatus.open
+ break
+ case TodoMarkedAsCompleteEvent.class:
+ this.status = TodoStatus.completed
+ break
+ default:
+ throw new IllegalArgumentException("Event ${domainEventMessage.payloadType} is not supported")
+ }
+
}
Object getIdentifier() {
return aggregateIdentifier
}
}
+
+enum TodoStatus {
+ open,completed
+}
@@ -0,0 +1,43 @@
+package aggregates
+
+import commands.CreateTodoCommand
+import org.axonframework.commandhandling.CommandHandler
+import org.axonframework.commandhandling.CommandMessage
+import org.axonframework.repository.Repository
+import org.axonframework.unitofwork.UnitOfWork
+import org.vertx.groovy.deploy.Container
+import commands.MarkTodoAsCompleteCommand
+
+/**
+ * This handler is used for handling all commands related to todoItems.
+ *
+ * @author Jettro Coenradie
+ */
+class TodoCommandHandler implements CommandHandler {
+ def log = container.logger
+
+ Repository<Todo> repository
+
+ TodoCommandHandler(Repository<Todo> repository, Container container) {
+ this.repository = repository
+ }
+
+ Object handle(CommandMessage commandMessage, UnitOfWork unitOfWork) {
+ switch(commandMessage.payloadType) {
+ case CreateTodoCommand.class:
+ CreateTodoCommand command = commandMessage.payload as CreateTodoCommand
+ log.info "The received command is ${command.todoText}"
+ def todo = new Todo(command.identifier, command.todoText)
+ repository.add(todo)
+ return todo
+ case MarkTodoAsCompleteCommand.class:
+ MarkTodoAsCompleteCommand command = commandMessage.payload as MarkTodoAsCompleteCommand
+ def load = repository.load(command.identifier)
+ load.markAsComplete()
+ return load
+ default:
+ log.info "Received a command of type we cannot handle: ${commandMessage.payloadType}"
+ return null
+ }
+ }
+}
@@ -2,7 +2,10 @@ package aggregates
import org.axonframework.domain.IdentifierFactory
import org.axonframework.common.Assert
+
/**
+ * The Identifier for the todoItems
+ *
* @author Jettro Coenradie
*/
class TodoIdentifier implements Serializable {
@@ -31,4 +34,11 @@ class TodoIdentifier implements Serializable {
int hashCode() {
return (identifier != null ? identifier.hashCode() : 0)
}
+
+ @Override
+ public String toString() {
+ return "TodoIdentifier{" +
+ "identifier='" + identifier + '\'' +
+ '}';
+ }
}
@@ -1,13 +1,17 @@
package commands
+import aggregates.TodoIdentifier
+
/**
+ * Command for creating a new todoItem with the provided todoText.
+ *
* @author Jettro Coenradie
*/
class CreateTodoCommand {
- String identifier
+ TodoIdentifier identifier
String todoText
- CreateTodoCommand(String identifier, String todoText) {
+ CreateTodoCommand(TodoIdentifier identifier, String todoText) {
this.identifier = identifier
this.todoText = todoText
}
@@ -0,0 +1,16 @@
+package commands
+
+import aggregates.TodoIdentifier
+
+/**
+ * Command that marks the todoItem with the provided identifier as complete.
+ *
+ * @author Jettro Coenradie
+ */
+class MarkTodoAsCompleteCommand {
+ TodoIdentifier identifier
+
+ MarkTodoAsCompleteCommand(TodoIdentifier identifier) {
+ this.identifier = identifier
+ }
+}
@@ -6,15 +6,16 @@ import aggregates.TodoIdentifier
* @author Jettro Coenradie
*/
class TodoCreatedEvent {
- TodoIdentifier aggregateIdentifier;
+ TodoIdentifier aggregateIdentifier
String todoText
- TodoCreatedEvent(TodoIdentifier identifier, String todoText) {
- this.aggregateIdentifier = identifier
+ TodoCreatedEvent(TodoIdentifier aggregateIdentifier, String todoText) {
+ this.aggregateIdentifier = aggregateIdentifier
this.todoText = todoText
}
TodoIdentifier getIdentifier() {
return aggregateIdentifier
}
+
}
@@ -0,0 +1,19 @@
+package events
+
+import aggregates.TodoIdentifier
+
+/**
+ * @author Jettro Coenradie
+ */
+class TodoMarkedAsCompleteEvent {
+ TodoIdentifier aggregateIdentifier
+
+ TodoMarkedAsCompleteEvent(TodoIdentifier identifier) {
+ this.aggregateIdentifier = identifier
+ }
+
+ TodoIdentifier getIdentifier() {
+ return aggregateIdentifier
+ }
+
+}
@@ -0,0 +1,51 @@
+package query
+
+import events.TodoCreatedEvent
+import events.TodoMarkedAsCompleteEvent
+import org.axonframework.domain.EventMessage
+import org.axonframework.eventhandling.EventListenerProxy
+import org.vertx.groovy.deploy.Container
+import org.vertx.groovy.core.eventbus.EventBus
+
+/**
+ * Listener that handles all events and creates messages to store the todoItems in the query database.
+ *
+ * @author Jettro Coenradie
+ */
+class TodoEventListener implements EventListenerProxy {
+ Container container
+ EventBus eventBus
+ def logger
+
+ TodoEventListener(EventBus eventBus, Container container) {
+ this.container = container
+ this.logger = container.logger
+ this.eventBus = eventBus
+ }
+
+ Class<?> getTargetType() {
+ return TodoCreatedEvent.class
+ }
+
+ void handle(EventMessage eventMessage) {
+ def identifier = eventMessage.getIdentifier()
+ def eventType = eventMessage.payloadType
+
+ logger.debug "Received an event with id ${identifier} and type ${eventType}"
+
+ switch (eventType) {
+ case TodoCreatedEvent.class:
+ logger.info "Received a TodoCreatedEvent"
+ def event = eventMessage.payload as TodoCreatedEvent
+ eventBus.publish("message.all.clients",["todoText": event.todoText,"identifier":event.identifier])
+ break
+ case TodoMarkedAsCompleteEvent.class:
+ logger.info "Received a TodoMarkedAsCompleteEvent"
+ def event = eventMessage.payload as TodoMarkedAsCompleteEvent
+ // TODO implement this functionality
+ break
+ default:
+ logger.info "Cannot handle this event"
+ }
+ }
+}
@@ -0,0 +1,29 @@
+import org.vertx.groovy.core.http.RouteMatcher
+import org.vertx.groovy.core.http.impl.DefaultHttpServer
+
+/**
+ * Web server to handle all incoming web requests.
+ *
+ * @author Jettro Coenradie
+ */
+
+def log = container.logger
+
+RouteMatcher routeMatcher = new RouteMatcher()
+
+routeMatcher.get("/") {req ->
+ req.response.sendFile "static/index.html"
+}
+
+routeMatcher.getWithRegEx("^\\/static\\/.*") { req ->
+ req.response.sendFile(req.path.substring(1))
+}
+
+DefaultHttpServer server = vertx.createHttpServer()
+server.requestHandler(routeMatcher.asClosure())
+
+vertx.createSockJSServer(server).bridge(prefix: '/eventbus', [[:]], [[:]])
+
+server.listen(8080)
+
+log.info "The http server is started"
Oops, something went wrong.

0 comments on commit c9b117b

Please sign in to comment.