Akka typed serialization

How can we Serialize/Deserialize ActorRef ?
Can we use “ActorRefResolver”

final ActorRef<T> myActor = ...
final String str = ActorRefResolver.get(system).toSerializationFormat(myActor);

That is correct. ActorRefResolver provides toSerializationFormat for serialization to String and resolveActorRef for deserialization from String.

1 Like

@patriknw

While serializing typed cluster remote ActorRef via ActorRefResolver there seems to be a potential problem.

Please refer the below example to serialize typed cluster remote ActorRef:

  implicit val timeout: Timeout = Timeout(5.seconds)
  private val system1 = ActorSystem(Behaviors.empty, "system-1")
  private val system2 = ActorSystem(Behaviors.empty, "system-2")
  
  private val actorRefOne: ActorRef[Nothing] = Await.result(system1.systemActorOf(Behaviors.empty, "actor-1"), 5.seconds)
  
  ActorRefResolver(system2).toSerializationFormat(actorRefOne)

Here, ActorRefResolver requires an ActorSystem and uses its default address for serializing actorRefOne. Hence, the user can make a mistake of providing some random ActorSystem (system2) other than the one to which actorRefOne belongs to i.e. system1. This problem remains unknown to the user until some runtime failure occurs.

This was not a problem in untyped api of serialization where default address was fetched from ActorRef directly

Serialization.serializedActorPath(actorRefOne.toUntyped)

So, shouldn’t we have the same safety like untyped api in ActorRefResolver as well ?

I think you are right that we can improve this. Please create an issue.

Thanks @patriknw. Will create an issue and follow up there.