An open API service indexing awesome lists of open source software.

https://github.com/daggerok/akka-persistence-json-serializaer-example

This example demonstrate how to setup akka jackson JSON serialization integration
https://github.com/daggerok/akka-persistence-json-serializaer-example

akka akka-actor akka-actors akka-applications akka-persistence jackson sbt sbt-wrapper sbtw scala

Last synced: 3 months ago
JSON representation

This example demonstrate how to setup akka jackson JSON serialization integration

Awesome Lists containing this project

README

        

# akka-persistence-json-serializaer-example [![Build Status](https://travis-ci.org/daggerok/akka-persistence-json-serializaer-example.svg?branch=master)](https://travis-ci.org/daggerok/akka-persistence-json-serializaer-example)
This example demonstrate how to setup akka jackson JSON serialization integration

_update `build.sbt` file:_

```scala
scalaVersion := "2.13.0"
val jacksonVersion = "2.9.9"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-persistence" % "2.5.23" ,
"com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion,
"com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion,
// ...
)
```

_create `src/main/scala/my/app/MySerializer.scala` file:_

```scala
import akka.serialization.JSerializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

object MySerializer {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
// other optional jackson configurations...
}

class MySerializer extends JSerializer {
import MySerializer._
override protected def fromBinaryJava(bytes: Array[Byte], manifest: Class[_]): AnyRef = mapper.readValue(bytes)
override def identifier: Int = 1234567
override def toBinary(o: AnyRef): Array[Byte] = mapper.writeValueAsBytes(o)
override def includeManifest: Boolean = false
}
```

_update `src/main/resources/application.conf` file:_

```scala
akka {
actor {
serialize-creators = on
enable-additional-serialization-bindings = on

serializers {
json = "my.app.MySerializer"
}

serialization-bindings {
"my.app.MyClass1" = json
"my.app.MyClass2" = json
// ...
"my.app.MyClassN" = json
}
}
}
```

_build, run and test current sample:_

```bash
./sbtw clean "runMain com.github.daggerok.akka.persistence.SnapshotExample"
# ...
cat target/persistence-snapshots/snapshot-*
```

## othrer reposaitories

- https://github.com/daggerok/akka-persistence-examples

## resources

- https://doc.akka.io/docs/akka/current/serialization.html
- https://doc.akka.io/docs/akka/2.5.2/java/serialization.html
- https://doc.akka.io/docs/akka/current/persistence.html
- https://doc.akka.io/docs/akka/snapshot/serialization-jackson.html
- https://doc.akka.io/docs/akka/1.2/scala/serialization.html
- https://index.scala-lang.org/nextgentel/akka-tools/akka-tools-jdbc-journal/1.1.0
- https://github.com/lagom/lagom/blob/master/jackson/src/main/resources/reference.conf
- [YouTube: Persistent Actor State](https://www.youtube.com/watch?v=QBj_N1DeRLs&list=PLMhMDErmC1TdBMxd3KnRfYiBV2ELvLyxN&index=41)

## generated (original) README.md
This tutorial contains examples that illustrate a subset of[Akka Persistence](http://doc.akka.io/docs/akka/2.5/scala/persistence.html) features.

- persistent actor
- persistent actor snapshots
- persistent actor recovery

Custom storage locations for the journal and snapshots can be defined in [application.conf](src/main/resources/application.conf).

### Persistent actor

[PersistentActorExample.scala](src/main/scala/com/github/daggerok/akka/persistence/PersistentActorExample.scala) is described in detail in the [Event sourcing](http://doc.akka.io/docs/akka/2.5/scala/persistence.html#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](src/main/scala/com/github/daggerok/akka/persistence/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](src/main/scala/com/github/daggerok/akka/persistence/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"`.