项目作者: daggerok

项目描述 :
This example demonstrate how to setup akka jackson JSON serialization integration
高级语言: Shell
项目地址: git://github.com/daggerok/akka-persistence-json-serializaer-example.git
创建时间: 2019-07-22T00:24:45Z
项目社区:https://github.com/daggerok/akka-persistence-json-serializaer-example

开源协议:MIT License

下载


akka-persistence-json-serializaer-example Build Status

This example demonstrate how to setup akka jackson JSON serialization integration

update build.sbt file:

  1. scalaVersion := "2.13.0"
  2. val jacksonVersion = "2.9.9"
  3. libraryDependencies ++= Seq(
  4. "com.typesafe.akka" %% "akka-persistence" % "2.5.23" ,
  5. "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion,
  6. "com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion,
  7. // ...
  8. )

create src/main/scala/my/app/MySerializer.scala file:

  1. import akka.serialization.JSerializer
  2. import com.fasterxml.jackson.databind.ObjectMapper
  3. import com.fasterxml.jackson.module.scala.DefaultScalaModule
  4. import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
  5. object MySerializer {
  6. val mapper = new ObjectMapper() with ScalaObjectMapper
  7. mapper.registerModule(DefaultScalaModule)
  8. // other optional jackson configurations...
  9. }
  10. class MySerializer extends JSerializer {
  11. import MySerializer._
  12. override protected def fromBinaryJava(bytes: Array[Byte], manifest: Class[_]): AnyRef = mapper.readValue(bytes)
  13. override def identifier: Int = 1234567
  14. override def toBinary(o: AnyRef): Array[Byte] = mapper.writeValueAsBytes(o)
  15. override def includeManifest: Boolean = false
  16. }

update src/main/resources/application.conf file:

  1. akka {
  2. actor {
  3. serialize-creators = on
  4. enable-additional-serialization-bindings = on
  5. serializers {
  6. json = "my.app.MySerializer"
  7. }
  8. serialization-bindings {
  9. "my.app.MyClass1" = json
  10. "my.app.MyClass2" = json
  11. // ...
  12. "my.app.MyClassN" = json
  13. }
  14. }
  15. }

build, run and test current sample:

  1. ./sbtw clean "runMain com.github.daggerok.akka.persistence.SnapshotExample"
  2. # ...
  3. cat target/persistence-snapshots/snapshot-*

othrer reposaitories

resources

generated (original) README.md

This tutorial contains examples that illustrate a subset ofAkka Persistence features.

  • persistent actor
  • persistent actor snapshots
  • persistent actor recovery

Custom storage locations for the journal and snapshots can be defined in application.conf.

Persistent actor

PersistentActorExample.scala is described in detail in the Event sourcing section of the user documentation. With every application run, the ExamplePersistentActor is recovered from events stored in previous application runs, processes new commands, stores new events and snapshots and prints the current persistent actor state to stdout.

To run this example, type sbt "runMain sample.persistence.PersistentActorExample".

Persistent actor snapshots

SnapshotExample.scala demonstrates how persistent actors can take snapshots of application state and recover from previously stored snapshots. Snapshots are offered to persistent actors at the beginning of recovery, before any messages (younger than the snapshot) are replayed.

To run this example, type sbt "runMain sample.persistence.SnapshotExample". With every run, the state offered by the most recent snapshot is printed to stdout, followed by the updated state after sending new persistent messages to the persistent actor.

Persistent actor recovery

PersistentActorFailureExample.scala shows how a persistent actor can throw an exception, restart and restore the state by replaying the events.

To run this example, type sbt "runMain sample.persistence.PersistentActorFailureExample".