Permalink
Browse files

Added persistent storage to the picture, now the todo items are store…

…d in a query database implemented in MongoDB.
  • Loading branch information...
1 parent b416f20 commit 18d107fc96e75ce49df4d5d088f1f2d1aff70655 @jettro committed Nov 20, 2012
@@ -1,10 +1,11 @@
-import org.vertx.groovy.core.eventbus.EventBus
-
def log = container.logger
-EventBus eventBus = vertx.eventBus
+def persisterConfig = ["db_name": "axon_todo_list", "address": "vertx.mongo.persist"]
container.with {
+ deployModule('vertx.mongo-persistor-v1.2', persisterConfig, 1) {
+ log.info "App: The mongo-persister module is deployed."
+ }
deployModule('web') {
log.info "App: The web module is deployed."
}
@@ -6,6 +6,7 @@ import org.axonframework.domain.EventMessage
import org.axonframework.eventhandling.EventListenerProxy
import org.vertx.groovy.core.eventbus.EventBus
import org.vertx.groovy.deploy.Container
+
/**
* Listener that handles all events and creates messages to store the todoItems in the query database.
*
@@ -34,17 +35,45 @@ class TodoEventListener implements EventListenerProxy {
switch (eventType) {
case TodoCreatedEvent.class:
- logger.info "Received a TodoCreatedEvent"
- def event = eventMessage.payload as TodoCreatedEvent
- eventBus.publish("message.all.clients", ["name": "TodoCreated", "todoText": event.todoText, "identifier": event.identifier.asString()])
+ handleTodoCreatedEvent(eventMessage)
break
case TodoMarkedAsCompleteEvent.class:
- logger.info "Received a TodoMarkedAsCompleteEvent"
- def event = eventMessage.payload as TodoMarkedAsCompleteEvent
- eventBus.publish("message.all.clients", ["name": "TodoCompleted", "identifier": event.identifier.asString()])
+ handleTodoMarkedAsCompletedEvent(eventMessage)
break
default:
logger.info "Cannot handle this event"
}
}
+
+ /* Helper methods */
+
+ private void handleTodoMarkedAsCompletedEvent(EventMessage eventMessage) {
+ logger.info "Received a TodoMarkedAsCompleteEvent"
+ def event = eventMessage.payload as TodoMarkedAsCompleteEvent
+ def mongoAction = [
+ "action": "update",
+ "collection": "todos",
+ "criteria": ["identifier": event.identifier.asString()],
+ "objNew": ["\$set": ["completed": true]],
+ "upsert": true,
+ "multi": false]
+ eventBus.send("vertx.mongo.persist", mongoAction) {
+ eventBus.publish("message.all.clients", ["name": "TodoCompleted", "identifier": event.identifier.asString()])
+ }
+ }
+
+ private void handleTodoCreatedEvent(EventMessage eventMessage) {
+ logger.info "Received a TodoCreatedEvent"
+ def event = eventMessage.payload as TodoCreatedEvent
+
+ def todoItem = [
+ "name": "TodoCreated",
+ "todoText": event.todoText,
+ "identifier": event.identifier.asString(),
+ "completed": false]
+ def mongoAction = ["action": "save", "collection": "todos", "document": todoItem]
+ eventBus.send("vertx.mongo.persist", mongoAction) {
+ eventBus.publish("message.all.clients", todoItem)
+ }
+ }
}
@@ -15,7 +15,7 @@
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
- <a class="brand" href="#">Vert.x sample</a>
+ <a class="brand" href="#">Vert.x-Axon sample</a>
</div>
</div>
</div>
@@ -30,6 +30,16 @@ function TodoListCtrl($scope, EventBus) {
}
$scope.$digest();
});
+
+ var mongoAction = {"action": "find", "collection": "todos", "matcher": {}};
+ EventBus.send("vertx.mongo.persist", mongoAction, function (reply) {
+ var msg = reply.results;
+ for (var i = 0; i < msg.length; i++) {
+ var todoItem = msg[i];
+ $scope.todoItems.push({"todoText": todoItem.todoText, "completed": todoItem.completed, "identifier": todoItem.identifier});
+ }
+ $scope.$digest();
+ });
$scope.$digest();
};
@@ -41,16 +51,16 @@ function TodoListCtrl($scope, EventBus) {
/* Controller methods */
$scope.addTodo = function () {
- publish(EventBus, "command.todo.create", {todoText: $scope.todoText});
+ send(EventBus, "command.todo.create", {todoText: $scope.todoText});
$scope.todoText = '';
};
$scope.markCompleted = function (todoItem) {
- publish(EventBus, "command.todo.markcompleted", {identifier: todoItem.identifier});
+ send(EventBus, "command.todo.markcompleted", {identifier: todoItem.identifier});
}
}
-function publish(eventbus, address, message) {
+function send(eventbus, address, message) {
eventbus.send(address, message, function (reply) {
/* Hiding an element is still hard with angularjs, so for now we do it the old fashioned way */
var replyMessageDiv = $('#replymessage');

0 comments on commit 18d107f

Please sign in to comment.