在Akka型中,如何在没有其ActorSystem的情况下反序列化序列化的ActorRef?

时间:2019-08-09 10:14:52

标签: scala protocol-buffers actor scalapb akka-typed

我对Akka World还是很陌生,而对Akka类型的人则少得多。也很抱歉,如果这个人很愚蠢,:(

因此,我遵循了请求-响应交互模式,以使参与者进行交互。这意味着在我的命令中添加actorRef。

但是我的命令全部在protobuf中。我不得不序列化actorRef。 在不了解actor2的ActorSystem的情况下,actor1如何反序列化actor2的actorRef?

我已阅读答案:(https://manuel.bernhardt.io/2018/07/20/akka-anti-patterns-java-serialization/#comment-157564) 但是我很难理解解决方案...:(

WorkgroupCommand.proto

message WorkgroupCommand {
    oneof sealed_value {
        EnqueueWorkDone enq = 1;
        QueueFull qF = 2;
    }
}

message EnqueueWorkDone {
    required string id = 1;
    required string replyTo = 2; //serialized actorRef
}

Workgroup.scala

private val system1 = ActorSystem(Behaviors.empty, "system-1")

val commandHandler:(State, WorkgroupCommand) => Effect[Event,State] ={
  case (state, EnqueueWorkDone(id, replyTo)) =>
     //how to deserialize replyTo ?????

}

Agent.scala

private val system2 = ActorSystem(mainBehavior, "system-2")
private val resolver = ActorRefResolver(system2.toTyped)

def mainBehavior = Behaviors.setup{ context => 
  //assuming the we have the Workgroup actor
  Workgroup ! EnqueueWorkDone("12345",
                resolver.toSerializationFormat(context.self))

  Behaviors.same
}

有办法吗? 如果工作组和代理都是主actorSystem的子代,是否有帮助?

1 个答案:

答案 0 :(得分:0)

val actorRefResolver = ActorRefResolver(system.toTyped)

然后

val serializedActorRef: Array[Byte]= actorRefResolver.toSerializationFormat(replyTo).getBytes(StandardCharsets.UTF_8)

val str = new String(serializedActorRef, StandardCharsets.UTF_8)

val deserializedActorRef = actorRefResolver.resolveActorRef[SomeType.type](str)