使用Akka实现CQRS/ES的源码

14-11-04 banq
                   

这是一篇介绍如何使用Akka实现CQRS和EventSourcing的文章,源码项目下载:https://github.com/eigengo/akka-csug

该源码中使用PersistentActor 作为CQRS中命令的处理,而PersistentView 作为查询的处理。

UserExerciseProcessor 是处理命令的一个案例,代码如下:

class UserExerciseProcessor extends PersistentActor {
  import AccelerometerData._
  import UserExerciseProtocol._

  private var buffer: BitVector = BitVector.empty

  override val persistenceId: String = "user-exercise-persistence"

  override val receiveRecover: Receive = Actor.emptyBehavior

  override def receiveCommand: Receive = ???

}
<p>

UserExerciseView 是处理显示查询:

class UserExerciseView extends PersistentView {
  import UserExerciseProtocol._
  import UserPushNotification._
  import actors._

  private var exercises: List[ClassifiedExercise] = Nil

  override val viewId: String = "user-exercise-view"

  override val persistenceId: String = "user-exercise-persistence"

  override def receive: Receive = ???
}
<p>