Skip to content

Instantly share code, notes, and snippets.

@johanandren
Last active June 8, 2017 11:22
Show Gist options
  • Save johanandren/87fec0b627996a3f850513b81e0a8d66 to your computer and use it in GitHub Desktop.
Save johanandren/87fec0b627996a3f850513b81e0a8d66 to your computer and use it in GitHub Desktop.
package basics
import akka.actor.{ActorSystem, ExtendedActorSystem}
import akka.serialization.{JavaSerializer, SerializationExtension}
import com.typesafe.config.ConfigFactory
trait MyPayload
trait OtherPayload
trait PersistentExecutorProtocol extends Serializable {
type Payload
sealed trait Event extends Serializable
sealed trait Todo { def value: Payload }
final case class Done(value: Long) extends Event
}
object AProtocolInstance extends PersistentExecutorProtocol {
type Payload = MyPayload
final case class ToDo(value: MyPayload) extends super.Todo
}
object BProtocolInstance extends PersistentExecutorProtocol {
type Payload = OtherPayload
final case class ToDo(value: OtherPayload) extends super.Todo
}
class SerializerA(system: ExtendedActorSystem) extends JavaSerializer(system)
class SerializerB(system: ExtendedActorSystem) extends JavaSerializer(system)
object SerializerAndPathDependentTypes extends App {
implicit val system = ActorSystem("sample", ConfigFactory.parseString(
"""
akka.actor {
serializers {
serializerA = "basics.SerializerA"
serializerB = "basics.SerializerB"
}
serialization-identifiers {
"basics.SerializerA" = 70
"basics.SerializerB" = 71
}
serialization-bindings {
"basics.AProtocolInstance$ToDo" = serializerA
"basics.BProtocolInstance$ToDo" = serializerB
}
}
"""))
val ser = SerializationExtension(system)
println("serializer for AProtocolInstance.ToDo: " + ser.serializerFor(classOf[AProtocolInstance.ToDo]).getClass)
println("serializer for BProtocolInstance.ToDo: " + ser.serializerFor(classOf[BProtocolInstance.ToDo]).getClass)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment