<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Orange11 Blog &#187; Axon</title>
	<atom:link href="http://blog.orange11.nl/tag/axon/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.orange11.nl</link>
	<description>Keep updated on what we&#039;re doing!</description>
	<lastBuildDate>Tue, 15 May 2012 08:59:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Refactoring in an event-sourced world - Upcasting in Axon 2</title>
		<link>http://blog.orange11.nl/2012/04/17/refactoring-in-an-event-sourced-world-upcasting-in-axon-2/</link>
		<comments>http://blog.orange11.nl/2012/04/17/refactoring-in-an-event-sourced-world-upcasting-in-axon-2/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 07:29:53 +0000</pubDate>
		<dc:creator>Frank Versnel</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Axon]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[upcasting]]></category>

		<guid isPermaLink="false">http://blog.orange11.nl/?p=4467</guid>
		<description><![CDATA[This article explains how the concept of upcasting can be applied to an event-sourced model. Focusing on demultiplexing of events, a new feature in Axon 2. Introduction All applications are subject to continuing change, meaning they have to "be continually adapted or they become progressively less satisfactory" [Lehman]. Refactoring is a common technique to prepare [...]]]></description>
			<content:encoded><![CDATA[<p>This article explains how the concept of upcasting can be applied to an event-sourced model. Focusing on demultiplexing of events, a new feature in Axon 2.<br />
<span id="more-4467"></span></p>
<h2>Introduction</h2>
<p>All applications are subject to continuing change, meaning they have to "be continually adapted or they become progressively less satisfactory" [Lehman]. Refactoring is a common technique to prepare the program's source code for newly incoming changes.</p>
<p>Refactoring in an event-sourced model is quite different from the more common object-relation model (ORM). In an event sourced model state is derived from events instead of being stored directly. To change the structure of an event means that a mechanism has to be provided to handle all events stored with the old structure. One of those mechanisms is called upcasting. This article will explain how upcasting can be used to facilitate refactoring in an event sourced model. We will use the Axon framework, a framework that helps applying the event-sourcing model, to explain the concepts of upcasting in detail. We'll focus on demultiplexing of events, a new feature that I implemented for the upcoming Axon version (Axon 2).</p>
<h2>Upcasting</h2>
<p>Originally a concept of object-oriented programming, where: "a subclass gets casted to it's superclass automatically when needed", upcasting can also be applied to event sourcing. Upcasting in an event-sourced model allows for events to be refactored while keeping the complete event history of the model (all previous state) intact. Saving the application's state this way brings several advantages (note that this list is not exhaustive):</p>
<ul>
<li>It can be used as an audit log, making problems easier to track down.</li>
<li>All state changes are made explicit by name (e.g BirthNameDetailsUpdatedEvent), making it easy to understand what the program does.</li>
<li>It allows you to keep track of historical changes, which might be relevant at a later stage in the development of your application. E.g. the client wants to perform some statistical analysis based on the usage of the application over time.</li>
</ul>
<p>The main disadvantage of using an event-sourced model is when code needs to get refactored. Performing a big refactoring on your code base is hard because you'll have to let your code work with all events that were fired by the old code base. To make refactoring possible when historical events do not fit your new model anymore is through upcasting.</p>
<p>Upcasters are manually written procedures that have one input event of revision x and output, typically one, new mutated event of revision x+1. Events already stored in the database prior to the moment of refactoring get transformed to the refactored version of that event through an upcaster. Upcasting in Axon is performed in a process before events are sent to the application, meaning that the rest of the application can be completely ignorant about outdated events.</p>
<p>Core to the process of upcasting in Axon is the UpcasterChain: a class containing all upcasters as specified by the caller in the order specified by the caller. The upcaster chain is responsible for upcasting incoming events and delegating each event to the upcasters in the specified order. The interface of the UpcasterChain looks like this:</p>
<pre><code>public class UpcasterChain { 

    private List&lt;Upcaster&gt; chain; 

    public List&lt;Event&gt; upcast(Event event);

    ...
}
</code></pre>
<p>The UpcasterChain combines upcasters so that one upcaster only needs to know about one event type of one specific revision. This allows the programmer to write upcasters that are small, isolated, and easy to understand.</p>
<h2>Upcasting and demultiplexing</h2>
<p>To demultiplex data is to take data from a single input channel and divide that data over multiple output channels. When we apply this concept to upcasting this means that one event can be transformed into multiple events through an upcaster.</p>
<p>Each upcaster indicates whether it can process the type of the event that needs upcasting. If the upcaster's type matches with the type of the incoming event the chain will pass the event to the upcaster. The upcaster will then process the event and return zero, one or multiple 'upcasted' events. Since upcasters are arranged in a chain all events coming from the upcaster are passed into all remaining upcasters that are still left in the chain, as shown in the following pseudo code:</p>
<pre><code>method upcast(chain, event):
    if chain is empty then
        no upcasting needs to be done return a singleton list
        with the given event
    else
        upcast the event with the first upcaster in the list, like this:
        processed events = chain.head.upcast(event) 

        process each upcasted event through all remaining upcasters
        in the chain (chain.tail) by recursively calling the upcast
        method on all these events and return the result as a list.
        this list will represents all fully upcasted events.
</code></pre>
<p>To illustrate how demultiplexing works in practice we'll explain a simple case of updating administrative details for a user:</p>
<p>It so happens to be that there is a system running in production containing many AdministrativeDetailsUpdated events. New requirements have let you to introduce two new events: AddressUpdatedEvent and InsuranceDetailsUpdatedEvent which are processed by different parts of the application. Previously though, all information in these two events was contained in the old AdministrativeDetailsUpdatedEvent, which is now deprecated. In Axon 1 it is not possible to handle the situation described in the example properly because upcasters cannot do demultiplexing. In other words, the granularity of old events cannot be changed through upcasting so there is no way of nicely separating the business logic of handling address changes and insurance details changes. Ultimately this will either make programmers reluctant in refactoring their model or it will make them introduce hacks to let it work, degrading the readability of the source code. In Axon 2 an upcaster can be implemented to transform these events into instances of AddressUpdatedEvent and InsuranceDetailsUpdatedEvent and pass them to the application code, solving the previously described problem.</p>
<h2>Conclusion</h2>
<p>We've introduced the concept of upcasting in an event-sourced application and we've shown how this concept is useful by providing a real world example. We've shown that upcasting in Axon 2 is more flexible from its Axon 1 counterpart due to its ability to demultiplex events.</p>
<h2>References</h2>
<p>[Lehman] Lehman, M. M. (1980). "On Understanding Laws, Evolution, and Conservation in the Large-Program Life Cycle". Journal of Systems and Software</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.orange11.nl/2012/04/17/refactoring-in-an-event-sourced-world-upcasting-in-axon-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The new axon incubator and the google app engine project</title>
		<link>http://blog.orange11.nl/2011/07/06/the-new-axon-incubator-and-the-google-app-engine-project/</link>
		<comments>http://blog.orange11.nl/2011/07/06/the-new-axon-incubator-and-the-google-app-engine-project/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 14:08:38 +0000</pubDate>
		<dc:creator>Jettro Coenradie</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Axon]]></category>
		<category><![CDATA[axonframework]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[incubator]]></category>

		<guid isPermaLink="false">http://blog.jteam.nl/?p=3582</guid>
		<description><![CDATA[Some time ago we started adding modules or classes to axon that were not really the core of the system. The most important one of them is a MongoDB based event store. The amount of testing of this module was not as high as the other parts. It has not yet been tested in a [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago we started adding modules or classes to axon that were not really the core of the system. The most important one of them is a MongoDB based event store. The amount of testing of this module was not as high as the other parts. It has not yet been tested in a production system. Still it was part of the core of Axon. Next to the MongoDB based event store we also developed a Google App Engine based event store. This was not in the core of axon, in fact it was only part of a sample project in Github. A few weeks a go we decided it would be better to add an incubator to axon and put these modules or classes that have not been tested as well as the core of axon in there. THe third module is the experimentation of Allard with a distributed command bus. Feel free to check out the code and use it or comment on it through the mailing list. But be warned that this code is under development and might therefore be less stable than the other parts of axon.</p>
<p>This blog post focusses on the Google App Engine module that can now be found in the incubator. I'll summarize the required changes to the axon framework so it can be used on google app engine. I did blog about this topic before, so I am not going into details for all the code, but point to the older blogpost where possible and focus on the new stuff.</p>
<p><span id="more-3582"></span><br />
<h2>Overview</h2>
<p>In <a href="http://blog.jteam.nl/2011/01/04/deploying-a-cqrs-application-based-on-the-axon-framework-on-google-app-engine/">my previous blogpost</a> I discussed the following topics.</p>
<ul>
<li>Gradle for building the sample</li>
<li>XStream (the used serializer) hacks required for google app engine. Things have changed in this area, I will discuss them later on.</li>
<li>Event store based on the RAW google app engine storage api. Only the package has changed.</li>
<li>Other technologies for the sample like: Spring web, Spring security and sitemesh</li>
<p>?</ul>
<p>So what has changed since that blog post?</p>
<h2>New dependencies</h2>
<p>We can now find the latest and greatest gae artifacts in the central repository and we have moved to the latest jdk (1.5.1). Next to the google app engine dependencies we now also have a dependency on the incubator module from axon. The following block shows the most important changes in the dependencies for the gradle build file. Have a look at the build.gradle file for the complete config.</p>
<pre>
    compile "org.axonframework:axon-core:$axonVersion",
            "org.axonframework.incubator:axon-googleappengine:1.1-SNAPSHOT",
            "com.google.appengine:appengine-api-1.0-sdk:$gaeVersion"
</pre>
<p>I also removed the gae lab dependency. This was required for the task api, but in 1.5.1 the task api has become a standard module for google app engine.</p>
<h2>Axon incubator module for google app engine</h2>
<p>What does the incubator module for google app engine provide? it contains additions for serialization, the event store and command/event bus implementations. In the future the event and command bus implementations will not be needed. We are going to make changes in the statistics part of axon.</p>
<h3>Serialization</h3>
<p>This part contains two classes. We have a utility class for Spring enabled applications. This is the XStreamFactory class. This factory initializes GaeExtream class with a PureJavaReflectionProvider. Some of the default providers that XStream usually uses are not supported on Google App Engine. Therefore we have our own GaeXStream class. When writing the previous blog post there was no way to provide your own instance of an XStream class. This has changed since then. Now we can provide our own XStream class. Therefore we do not need the GaeGenericXStreamSerializer and XStreamEventSerializer anymore.</p>
<h3>Event store</h3>
<p>Not a lot to mention here. I was pretty detailed in the other blog post. We have three classes required for the event store. We have the object to store, the EventEntry. Next to the entry we have The GaeEventStore and the GaeSnapshotter.</p>
<h3>Event and Command bus</h3>
<p>In the current snapshot version of axon, the SimpleCommandBus and the SimpleEventBus are MBean enabled. This class is not in the supported classes of GoogleAppEngine. Therefore we have to create our own event and command bus. They both are exact copies without the marker interface and the statistics stuff.</p>
<h2>The sample project</h2>
<p>The CompanyHr project is a google app engine project that makes use of axon. The google app engine hacks are now in the axon incubator module. Therefore we have removed them from the sample project. The sample makes use of basic spring mvc and it integrates the spring security module with google authentication.</p>
<p>All web related technologies have been mentioned in the previous blog post. Therefore I do not explain them here again.</p>
<h2>Concluding</h2>
<p>Integrating with Google app engine is not hard. We do have to make a few changes that are supported by axon. The only thing that is not very nice is the statistics problems. But we will come with a solution in that area as well.</p>
<p>If you still have questions or remarks, feel free to post a comment or use the axon user group.</p>
<h2>References</h2>
<ul>
<li><a href="http://blog.jteam.nl/2011/01/04/deploying-a-cqrs-application-based-on-the-axon-framework-on-google-app-engine/">http://blog.jteam.nl/2011/01/04/deploying-a-cqrs-application-based-on-the-axon-framework-on-google-app-engine/</a></li>
<li><a href="https://github.com/jettro/CompanyHr">https://github.com/jettro/CompanyHr</a></li>
<li><a href="http://code.google.com/p/axonframework/source/browse/trunk#trunk%2Fincubator%2Fgoogle-app-engine">http://code.google.com/p/axonframework/source/browse/trunk#trunk%2Fincubator%2Fgoogle-app-engine</a></li>
<p>?</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.orange11.nl/2011/07/06/the-new-axon-incubator-and-the-google-app-engine-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploying a CQRS application based on the Axon framework on Google App Engine</title>
		<link>http://blog.orange11.nl/2011/01/04/deploying-a-cqrs-application-based-on-the-axon-framework-on-google-app-engine/</link>
		<comments>http://blog.orange11.nl/2011/01/04/deploying-a-cqrs-application-based-on-the-axon-framework-on-google-app-engine/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 08:30:19 +0000</pubDate>
		<dc:creator>Jettro Coenradie</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Axon]]></category>
		<category><![CDATA[Axon Framework]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Objectify]]></category>
		<category><![CDATA[Spring MVC]]></category>

		<guid isPermaLink="false">http://blog.jteam.nl/?p=2774</guid>
		<description><![CDATA[The Axon framework is a CQRS framework created by Allard Buijze. From the beginning I am trying to help him out, mostly by creating samples. This blog post is about a sample I am creating for Google App Engine. Everybody that has been working with Google App Engine (GAE) knows that it enforces some limitations [...]]]></description>
			<content:encoded><![CDATA[<p>The Axon framework is a CQRS framework created by Allard Buijze. From the beginning I am trying to help him out, mostly by creating samples. This blog post is about a sample I am creating for Google App Engine. Everybody that has been working with Google App Engine (GAE) knows that it enforces some limitations on the jdk. The sample uses spring, spring security, sitemesh, objectify and of course Axon. I'll discuss everything that was needed to deploy a working axon application.</p>
<p><span id="more-2774"></span><br />
<h2>Introduction</h2>
<p>Let us have a high level look at what I had to do to make the sample work.</p>
<h3>Axon framework</h3>
<p>If you are not familiar with Axon and want to learn more about axon, this is not the best blogpost to start with. Start reading the excellent <a href="http://www.axonframework.org/docs/">reference manual of Axon</a>. If you want to create your own application with Axon on GAE, you have to use the lastest and greatest version of Axon. I am working with the 0.7-SNAPSHOT and I had to patch it a little bit. More on this later on, but if you are interested you can <a href="http://code.google.com/p/axonframework/issues/detail?id=107">follow this issue</a>.</p>
<p>There are a few things I have created to make Axon run on GAE. For starters, I needed an event store and a query database that make use of the google app engine data store. The other thing I needed was a snapshotter. Like the name says, the snapshotter has the responsibility to initiate creating a snapshot of an aggregate root. The snapshotter makes use of the GAE Task Queues.</p>
<p>By default, Axon makes use of XStream. Although you can configure Axon to use something else, I have chosen to use XStream. XStream and GAE are not best friends, I had to create some tweaks to make it work.</p>
<h3>Objectify</h3>
<p>I started of using jpa to store everything in the GAE datastore. I had tried this path in the past, but it did not feel right. For the event store I decided to use the low level api. For the query database I wanted to make my life easier. Therefore I decided to look for a framework. I found <a href="http://code.google.com/p/objectify-appengine/">Objectify</a> to give what I needed.</p>
<blockquote><p>Objectify-Appengine provides the thinnest convenient layer which addresses these issues, yet preserves the elegance of get, put, delete, and query.</p></blockquote>
<p>Because I want to store the EventEntry objects in different collections based on their type I did not see advantages in using Objectify for the EventStore as well. Therefore I sticked to the low level api of the data store.</p>
<h3>Spring security</h3>
<p>For the application I am creating I want to implement security. Since I am used to Spring security I wanted to use spring security. I found <a href="http://blog.springsource.com/2010/08/02/spring-security-in-google-app-engine/">an excellent blog post</a> and a sample in the trunk for spring security that helped me a lot.</p>
<h3>Other tools</h3>
<p>Other tools I used that require less attention are:</p>
<ul>
<li>Spring web</li>
<li>Gradle - for building the sample</li>
<li>Intellij - for writing code and running the local GAE instance</li>
<li>Gae SDK - version 1.4 at the time of writing</li>
<li>Sitemesh - For templating the pages</li>
<li>jQuery - To make the sample look just a little bit better</li>
<p>?</ul>
<h2>The sample</h2>
<p>I decided to start this sample to automate some of the basic processes around Human Resource Management in a smaller company. I am starting of with a CV creating tool. I am not finished by far. Actually I have just started and I focussed on getting it to work. If you want to have a look at the code, you can find it at Github.</p>
<p><a href="https://github.com/jettro/CompanyHr">https://github.com/jettro/CompanyHr</a></p>
<p>I use gradle to build the sample. Tried to find a good GAE plugin, but did not really succeed. Therefore I build with gradle, generate the intellij project files with gradle and run the developer version of GAE from intellij. Uploading the sample is done using the command line utilities provided by the google app engine sdk. If you are interested in gradle or want to build the sample yourself, have a look at the next section. If you are not interested in building the sample, it is safe to skip this section.</p>
<h3>Gradle</h3>
<p>First you need to be able to find all the dependencies. The section that configures the repositories, shows a few gradle tricks. The first repo to use is you local maven repository. Beware, if you do not keep your maven repository in you home folder under .m2/repository this will not work. The second trick is about finding the google app engine jars. I use the feature to use a flat directory as a repository. You can configure the location of google app engine in the gradle.properties file. Finally we use the central maven repository and some additional repositories for axon snapshots and objectify. The following code block shows the configuration.</p>
<pre class="brush: groovy; title: ; notranslate">
repositories {
    mavenRepo urls: [&quot;file://&quot; + System.getProperty('user.home') + &quot;/.m2/repository/&quot;]
    flatDir(dirs: &quot;$gaeHome/lib/&quot;)
    mavenCentral()
    mavenRepo urls: [
            &quot;http://oss.sonatype.org/content/groups/public&quot;,
            &quot;http://objectify-appengine.googlecode.com/svn/maven&quot;]
}
</pre>
<p>The other bit of gradle I want to mention is the exploded war file. The google upload utility needs this. We unzip the war to an external folder as a last stage of the war creation. The following code block shows the configuration of the exploded directory.</p>
<pre class="brush: groovy; title: ; notranslate">
war.doLast {
    ant.unzip src: war.archivePath, dest: explodedWar
}
</pre>
<p>Now you can build the project using gradle: <em>gradle clean build</em>. Or you do <em>gradle idea</em> and run everything from intellij.</p>
<h2>XStream hacks</h2>
<p>As I mentioned before, axon uses XStream to serialize and deserialize objects. A lot of reflection power is used to re-instantiate serialized objects. With GAE there are some limitations in this area. The most important change is in the XStream class, this is the most used class with the XStream library. Within this class Converters are registered. Some of these converters do not work on google app engine. Therefore I have created a subclass of XStream called GaeXStream. I removed the dynamic loading of converters as well as the jdk specific configurations. You can find the classes in the package <em>nl.gridshore.companyhr.axongeahacks</em>.</p>
<p>Axon provides a GenericXStreamSerializer, this class initializes an XStream object. At the moment there is no way to provide your own XStream object. Maybe in the future we can provide a constructor for this class that accepts an XStream object. For now I have created my own implementation of this class,
<li>GaeGenericXStreamSerializer</li>
<p>.</p>
<p>The final class I had to change was the
<li>XStreamEventSerializer</li>
<p>. This class creates the GenericXStreamSerializer. Again there is no real mechanism to change this construction. I have copied the original class and changed the line where the GenericXStreamSerializer is created into the creation of my GaeGenericXStreamSerializer. My own class is called GaeXStreamEventSerializer.</p>
<p>In the future I'll try to simplify this and maybe change Axon to make this a little bit easier. For now this is working.</p>
<h2>Axon Event Store</h2>
<p>I already created an EventStore that uses MongoDB. Check my previous blog posts on gridshore.</p>
<p><a href="http://www.gridshore.nl/2010/09/20/learning-mongodb/">http://www.gridshore.nl/2010/09/20/learning-mongodb/</a><br/><br />
<a href="http://www.gridshore.nl/2010/09/27/still-learning-mongodb/">http://www.gridshore.nl/2010/09/27/still-learning-mongodb/</a></p>
<p>Allard improved the code for the MongoDB implementation and now I can reuse the ideas for the Google App Engine Event Store.</p>
<h3>EventEntry</h3>
<p>The EventEntry is the representation of what we store in the google data store. It has a few fields like aggregateIdentifier and the serializedEvent. The serialized event is our actual content serialized using XStream. The EventEntry also contains some methods to help us with the translation to google data store Entity objects and queries. To give you an idea, the following code block shows you the method to create a query for returning all Entities based on the provided aggregateIdentifier after the provided sequence number. You can find the <a href="https://github.com/jettro/CompanyHr/blob/master/src/main/java/nl/gridshore/companyhr/app/axon/EventEntry.java">sourcecode for the complete class here</a></p>
<pre class="brush: java; title: ; notranslate">
static Query forAggregate(String type, String aggregateIdentifier, long firstSequenceNumber) {
    return new Query(type)
            .addFilter(AGGREGATE_IDENTIFIER, Query.FilterOperator.EQUAL, aggregateIdentifier)
            .addFilter(SEQUENCE_NUMBER, Query.FilterOperator.GREATER_THAN_OR_EQUAL, firstSequenceNumber)
            .addSort(SEQUENCE_NUMBER, Query.SortDirection.ASCENDING);
}
</pre>
<h3>EventStore</h3>
<p>For the GAE event store I implement the <em>org.axonframework.eventstore.SnapshotEventStore</em> interface. The implementation supports storing the events as well as working with snapshot events. GAE provides the <em>DatastoreServiceFactory</em>. From this factory we obtain the ServiceFactory. The event store makes use of the XStream event serializer that was discussed before in this post. All aggregate types are stored in separate collections. We could store them all in the same collection, but according to google tips you should try to limit the amount of items in a collection and especially the amount of queries performed on a collection. We also store the snapshots in a different collection with respect to the normal events. The following image shows you the collections my app currently has.</p>
<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://blog.jteam.nl/wp-content/uploads/2011/01/Screen-shot-2011-01-03-at-08.50.27.png" alt="Screen shot 2011-01-03 at 08.50.27.png" border="0" width="406" height="215" /></p>
<p>As you can see, project has three tables. Project, snapshot_Project and ProjectEntry. Project contains the events, snapshot_Project contains the snapshots and ProjectEntry contains the projects from the query database perspective.</p>
<p>Now let us have a look at the implementation. You can find the code here: <a href="https://github.com/jettro/CompanyHr/blob/master/src/main/java/nl/gridshore/companyhr/app/axon/GaeEventStore.java">nl.gridshore.companyhr.app.axon.GaeEventStore</a>. When writing to the collections you have to understand transactions and google app engine. GAE does support transactions, but only within one aggregate root. Therefore we have to create new transactions for all events that we need to store, they are all aggregate roots. We could change this and create one document with all its events as an aggregate root, but I did not choose that direction. Appending an event now consists of a few steps. Loop over all provided DomainEvents, convert them into EventEntry objects, create transaction, store objects within transaction and commit. The following code block shows these steps in code. In the real code base this is  refactored, but this is easier to read.</p>
<pre class="brush: java; title: ; notranslate">
public void appendEvents(String type, DomainEventStream events) {
    while (events.hasNext()) {
        DomainEvent event = events.next();
        EventEntry entry = new EventEntry(type, event, eventSerializer);
        Transaction transaction = datastoreService.beginTransaction();
        try {
            datastoreService.put(transaction, entry.asEntity());
            transaction.commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }
    }
}
</pre>
<p>Storing snapshots is done in the exact same way. Loading an aggregate root is done using all events or all events starting from a snapshot event. To give you an idea about reading objects using GAE api, I'll show you how to load the last snapshot event for an aggregate root. The query object is created using the special factory method in EventEntry, then we prepare the object and iterate over the results of the query. Only the most recent snapshot is needed. Check the following code.</p>
<pre class="brush: java; title: ; notranslate">
// from GaeEventStore
private EventEntry loadLastSnapshotEvent(String type, AggregateIdentifier identifier) {
    Query query = EventEntry.forLastSnapshot(&quot;snapshot_&quot; + type, identifier.asString());
    PreparedQuery preparedQuery = datastoreService.prepare(query);
    Iterator&lt;Entity&gt; entityIterator = preparedQuery.asIterable().iterator();
    if (entityIterator.hasNext()) {
        Entity lastSnapshot = entityIterator.next();
        return new EventEntry(lastSnapshot);
    }
    return null;
}

// from EventEntry
static Query forLastSnapshot(String type, String aggregateIdentifier) {
    return new Query(type)
            .addFilter(AGGREGATE_IDENTIFIER, Query.FilterOperator.EQUAL, aggregateIdentifier)
            .addSort(SEQUENCE_NUMBER, Query.SortDirection.ASCENDING);
}
</pre>
<p>The other part I want to have a look at, is the next step in recovering an aggregate root. Reading all events that happened after a snapshot event. The next code block shows the most important part for the implementation.</p>
<pre class="brush: java; title: ; notranslate">
private List&lt;DomainEvent&gt; readEventSegmentInternal(String type, AggregateIdentifier identifier,
                                                   long firstSequenceNumber) {
    Query query = EventEntry.forAggregate(type, identifier.asString(), firstSequenceNumber);
    PreparedQuery preparedQuery = datastoreService.prepare(query);
    List&lt;Entity&gt; entities = preparedQuery.asList(FetchOptions.Builder.withDefaults());

    List&lt;DomainEvent&gt; events = new ArrayList&lt;DomainEvent&gt;(entities.size());
    for (Entity entity : entities) {
        byte[] bytes = ((Text) entity.getProperty(&quot;serializedEvent&quot;)).getValue().getBytes(EventEntry.UTF8);
        events.add(eventSerializer.deserialize(bytes));
    }
    return events;
}
</pre>
<p>Since a serialized event can become big, we store it in a Text field. As you can see we need some steps to extract the actual content. Other than that the code is easy to follow I hope.</p>
<p>I did not implement the <em>org.axonframework.eventstore.EventStoreManagement</em> interface yet. I Will do this in the nearby future. </p>
<p>With the EventStore implementation we can store and read events as well as snapshot events. Axon also has support for deciding when to create a snapshot. Google app engine provides the tasks api. Therefore I wanted to have an implementation of the <em>org.axonframework.eventsourcing.Snapshotter</em> interface that makes use of the task api.</p>
<h3>Snapshotter</h3>
<p>Creating your own snapshotter is not hard, it is an easy interface with just one method. The next code block shows you the implementation of the interface.</p>
<pre class="brush: java; title: ; notranslate">
public void scheduleSnapshot(String typeIdentifier, AggregateIdentifier aggregateIdentifier) {
    logger.debug(&quot;Schedule a new task to create a snapshot for type {} and aggregate {}&quot;,
            typeIdentifier, aggregateIdentifier);

    Queue queue = QueueFactory.getQueue(&quot;snapshotter&quot;);

    queue.add(TaskOptions.Builder.withUrl(&quot;/task/snapshot&quot;)
            .param(&quot;typeIdentifier&quot;, typeIdentifier)
            .param(&quot;aggregateIdentifier&quot;, aggregateIdentifier.asString())
            .method(TaskOptions.Method.POST)
    );
}
</pre>
<p>As you can see, the code is very easy. We do a post to a certain url with the parameters typeIdentifier and aggregateIdentifier. Then in a controller we intercept this url and call the following method.</p>
<pre class="brush: java; title: ; notranslate">
public void createSnapshot(String typeIdentifier, String aggregateIdentifier) {
    DomainEventStream eventStream =
            eventStore.readEvents(typeIdentifier, new StringAggregateIdentifier(aggregateIdentifier));
    DomainEvent snapshotEvent = createSnapshot(typeIdentifier, eventStream);
    if (snapshotEvent != null) {
        eventStore.appendSnapshotEvent(typeIdentifier, snapshotEvent);
    }
}

private DomainEvent createSnapshot(String typeIdentifier, DomainEventStream eventStream) {
    AggregateFactory&lt;?&gt; aggregateFactory = aggregateFactories.get(typeIdentifier);

    DomainEvent firstEvent = eventStream.peek();
    AggregateIdentifier aggregateIdentifier = firstEvent.getAggregateIdentifier();

    EventSourcedAggregateRoot aggregate = aggregateFactory.createAggregate(aggregateIdentifier, firstEvent);
    aggregate.initializeState(eventStream);

    return new AggregateSnapshot&lt;EventSourcedAggregateRoot&gt;(aggregate);
}
</pre>
<p>That is all not to hard, now we have an a-synchronous snapshotter. An alternative would be to use the standard axon implementations with the <em>org.axonframework.util.DirectExecutor</em> and the <em>org.axonframework.eventsourcing.SpringAggregateSnapshotter</em>. I leave this as an exercise to yourself <img src='http://blog.orange11.nl/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2>Concluding</h2>
<p>That is it, this is what is required to use axon on google app engine. Now you can configure the CommandBus, the EventBus, repositories, snapshotters, etc. In future posts I'll have a more detailed look at some of the other aspects of the sample like Objectify and the spring security implementation. If you want more information on certain topics, feel free to ask for it in the comment section. I'll answer your questions and could even write a new blog post if the answer becomes to big <img src='http://blog.orange11.nl/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.orange11.nl/2011/01/04/deploying-a-cqrs-application-based-on-the-axon-framework-on-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

