Hi
I am testing akka persistence with eventstore.
My problem is that i can’t seem to figure out what akka persistence saves as event data.
My events are ScalaPB classes, and should be serialized to protobuffer. But i am having difficulty deseralizing them, in my own code. Akka persistence works as it should.
It seems to be protobuffer binary format , but is my event wrapped in some other class?
I have tried:
val serializa1tion = SerializationExtension(context.system)
val event = serialization.deserialize(e.linkedEvent.data.data.value.toArray, myeventclass)
But then it tries java serialization and fails.
Some help would be great.
Your event is wrapped by PersistentRepr, so you need to account for that.
Try this:
val serialization: Serialization = SerializationExtension(context.system)
def deserialize[A](bytes: Array[Byte])(implicit ct: ClassTag[A]): Try[A] =
serialization.deserialize(bytes, ct.runtimeClass).asInstanceOf[Try[A]]
val x = for {
pr <- deserializePersistentRepr
} yield pr.payload
I lifted the protobuf serializer from akka-remoting and adjusted it to work with ScalaPB, see here: https://gist.github.com/ahjohannessen/ce43cf45607b9dd9050b
Furthermore, then you also need to instruct Akka serialization to use that serializer, like this:
akka {
actor {
serializers {
proto = "your.namespace.ProtobufSerializer"
}
serialization-bindings {
"com.trueaccord.scalapb.GeneratedMessage" = proto
"java.io.Serializable" = none // because it is a bad default
}
}
}
Hopefully that should get you going
Thanks this was exactly what i was looking for.