diff --git a/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/PingSerializerExampleTest.java b/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/PingSerializerExampleTest.java new file mode 100644 index 0000000000..9910c4ccf6 --- /dev/null +++ b/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/PingSerializerExampleTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2018 Lightbend Inc. + */ + +package jdocs.akka.cluster.typed; + +import akka.actor.ExtendedActorSystem; +import akka.actor.typed.ActorRef; +import akka.actor.typed.ActorRefResolver; +import akka.actor.typed.javadsl.Adapter; +import akka.serialization.SerializerWithStringManifest; + +import java.io.NotSerializableException; +import java.nio.charset.StandardCharsets; + +public class PingSerializerExampleTest { + + public class Pong {} + + public class Ping { + public final akka.actor.typed.ActorRef replyTo; + + public Ping(ActorRef replyTo) { + this.replyTo = replyTo; + } + } + + //#serializer + public class PingSerializer extends SerializerWithStringManifest { + + final ExtendedActorSystem system; + final ActorRefResolver actorRefResolver; + + static final String PING_MANIFEST = "a"; + static final String PONG_MANIFEST = "b"; + + PingSerializer(ExtendedActorSystem system) { + this.system = system; + actorRefResolver = ActorRefResolver.get(Adapter.toTyped(system)); + } + + @Override + public int identifier() { + return 97876; + } + + @Override + public String manifest(Object obj) { + if (obj instanceof Ping) + return PING_MANIFEST; + else if (obj instanceof Pong) + return PONG_MANIFEST; + else + throw new IllegalArgumentException("Unknown type: " + obj); + } + + @Override + public byte[] toBinary(Object obj) { + if (obj instanceof Ping) + return actorRefResolver.toSerializationFormat(((Ping) obj).replyTo).getBytes(StandardCharsets.UTF_8); + else if (obj instanceof Pong) + return new byte[0]; + else + throw new IllegalArgumentException("Unknown type: " + obj); + } + + @Override + public Object fromBinary(byte[] bytes, String manifest) throws NotSerializableException { + if (PING_MANIFEST.equals(manifest)) { + String str = new String(bytes, StandardCharsets.UTF_8); + ActorRef ref = actorRefResolver.resolveActorRef(str); + return new Ping(ref); + } else if (PONG_MANIFEST.equals(manifest)) { + return new Pong(); + } else { + throw new NotSerializableException("Unable to handle manifest: " + manifest); + } + } + } + //#serializer +} diff --git a/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/PingSerializer.scala b/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/PingSerializer.scala index dbfe38e500..b50fe18565 100644 --- a/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/PingSerializer.scala +++ b/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/PingSerializer.scala @@ -28,7 +28,7 @@ class PingSerializer(system: ExtendedActorSystem) extends SerializerWithStringMa override def toBinary(msg: AnyRef) = msg match { case Ping(who) ⇒ - ActorRefResolver(system.toTyped).toSerializationFormat(who).getBytes(StandardCharsets.UTF_8) + actorRefResolver.toSerializationFormat(who).getBytes(StandardCharsets.UTF_8) case Pong ⇒ Array.emptyByteArray } diff --git a/akka-docs/src/main/paradox/typed/cluster.md b/akka-docs/src/main/paradox/typed/cluster.md index 4efecdae93..372afd15af 100644 --- a/akka-docs/src/main/paradox/typed/cluster.md +++ b/akka-docs/src/main/paradox/typed/cluster.md @@ -110,4 +110,5 @@ For example here's how a serializer could look for the `Ping` and `Pong` message Scala : @@snip [PingSerializer.scala]($akka$/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/PingSerializer.scala) { #serializer } - +Java +: @@snip [PingSerializerExampleTest.java]($akka$/akka-cluster-typed/src/test/java/jdocs/akka/cluster/typed/PingSerializerExampleTest.java) { #serializer }