Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package my.akka.typed
import java.io.{PrintWriter, StringWriter}
import akka.serialization.{SerializationExtension, SerializerWithStringManifest}
import akka.typed.scaladsl._
import akka.typed.scaladsl.adapter._
import com.typesafe.config.{Config, ConfigFactory}
object MySerializer {
def serializeTypedActorRef(config: Config): Unit = {
val empty = Actor.immutable[String] { (_, msg)
akka.typed.scaladsl.Actor.same
}
val system = akka.actor.ActorSystem("example", config)
try {
val original = system.spawn(empty, "empty")
val serialization = SerializationExtension(system)
val serializer = serialization.findSerializerFor(original)
//Serializer for class akka.typed.internal.adapter.ActorRefAdapter = akka.serialization.JavaSerializer@7389af41
println(s"Serializer for ${original.getClass} = " + serializer)
// Turn it into bytes
val bytes = serializer.toBinary(original)
// Turn it back into an object
val back = serialization.findSerializerFor(original) match {
case s: SerializerWithStringManifest s.fromBinary(bytes, s.manifest(original))
case _ => serializer.fromBinary(bytes, manifest = None)
}
// Voilá!
println(back, " ", back.getClass)
println(original, " ", original.getClass)
} catch {
case t: Throwable => {
val sw = new StringWriter
t.printStackTrace(new PrintWriter(sw))
println(t.getMessage)
println(sw)
}
} finally {
system.terminate()
}
}
def main(args: Array[String]): Unit = {
serializeTypedActorRef(
ConfigFactory.parseString(
"""akka {
| actor {
| serialization-bindings {
| "akka.typed.ActorRef" = java
| "akka.typed.internal.adapter.ActorRefAdapter" = java
| }
| }
|}
""".stripMargin)
)
println("------------------------------------------------------")
serializeTypedActorRef(ConfigFactory.parseString(""))
}
}
Serializer for class akka.typed.internal.adapter.ActorRefAdapter = akka.serialization.JavaSerializer@7364a7de
akka.typed.internal.adapter.ActorRefAdapter
java.io.NotSerializableException: akka.typed.internal.adapter.ActorRefAdapter
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at akka.serialization.JavaSerializer.$anonfun$toBinary$1(Serializer.scala:319)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
at akka.serialization.JavaSerializer.toBinary(Serializer.scala:319)
at my.akka.typed.MySerializer$.serializeTypedActorRef(MySerializer.scala:27)
at my.akka.typed.MySerializer$.main(MySerializer.scala:53)
at my.akka.typed.MySerializer.main(MySerializer.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sbt.Run.invokeMain(Run.scala:67)
at sbt.Run.run0(Run.scala:61)
at sbt.Run.sbt$Run$$execute$1(Run.scala:51)
at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:55)
at sbt.Run$$anonfun$run$1.apply(Run.scala:55)
at sbt.Run$$anonfun$run$1.apply(Run.scala:55)
at sbt.Logger$$anon$4.apply(Logger.scala:84)
at sbt.TrapExit$App.run(TrapExit.scala:248)
at java.lang.Thread.run(Thread.java:745)
------------------------------------------------------
Serializer for class akka.typed.internal.adapter.ActorRefAdapter = akka.typed.cluster.internal.MiscMessageSerializer@520d16bb
(Actor[akka://example/user/empty#1537921913], ,class akka.typed.internal.adapter.ActorRefAdapter)
(Actor[akka://example/user/empty#1537921913], ,class akka.typed.internal.adapter.ActorRefAdapter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment