Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 925
daydreamer Avatar asked Jul 03 '16 01:07

daydreamer


2 Answers

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.

like image 116
thwiegan Avatar answered Nov 10 '22 00:11

thwiegan


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
 }
}
like image 41
Kunal Kanojia Avatar answered Nov 09 '22 23:11

Kunal Kanojia