简体   繁体   中英

How to change default Serializer for an Akka application?

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
}

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.

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
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM