How to change default Serializer for an Akka application?
Asked Answered
H

2

5

I read the akka serialization page, where they talk about serialization-bindings as

serialization-bindings {
      "java.lang.String" = java
      "docs.serialization.Customer" = java
      "com.google.protobuf.Message" = proto
      "docs.serialization.MyOwnSerializable" = myown
      "java.lang.Boolean" = myown
    }

I am considering use of kyro serialization and wondering if there is a way to turn on kyro serialization by default for complete application? rather than giving every class or package name?

Something like

serialization-bindings {
     default = kyro
}
Homogenetic answered 3/7, 2016 at 1:17 Comment(0)
G
7

This is the way, I use kryo:

serializers {
  kryo = "com.romix.akka.serialization.kryo.KryoSerializer"
}

serialization-bindings {
  "com.ex.es.Msg" = kryo
  "java.io.Serializable" = none
}

Similar to Kunal all my messages, that I send over the wire / store / serialize in any way extend that Msg class. That's not only because I'm not sure wether there is a way to set a default serializer, but also to make it explicit for me, what is serialized in my application.

And now to give your actual question a try:

I also had to set "java.io.Serializable" = none otherwise I got a warning, that it wasn't sure, which serializer to pick for my classes.

So this could imply, that case classes by default extend Serializable and make something like this work as a default setting:

serializers {
  kryo = "com.romix.akka.serialization.kryo.KryoSerializer"
}

serialization-bindings {
  "java.io.Serializable" = kryo
}

But that is only an educated guess and at the moment I have no possibility to test.

Gulgee answered 3/7, 2016 at 8:8 Comment(0)
C
3

The way I do that is to make all my serializable classes extend a SerializableTrait and then in my bindings I just specify my trait to be serialized using the custom serializer.

akka.actor {
  serializers {
    json-serializer = "io.example.MySerializerClass"
 }
  serialization-bindings {
   "io.example.SerializableTrait" = json-serializer
 }
}
Costanzo answered 3/7, 2016 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.