我正在尝试通过protobuf序列化/反序列化ActorRef。根据Akka的文档,唯一的方法是将ActorRef转换为String,然后将其转换回远程actor系统。
该文档提到使用ExtendedActorSystem
进行反序列化(请参见here)。但是,尚不清楚如何获取ExtendedActorSystem
:
// Serialize
// (beneath toBinary)
val identifier: String = Serialization.serializedActorPath(theActorRef)
// Then just serialize the identifier however you like
// Deserialize
// (beneath fromBinary)
// ==== Where is this extendedSystem from? ====
val deserializedActorRef = extendedSystem.provider.resolveActorRef(identifier)
// Then just use the ActorRef
编辑
[我在这里找到了这个问题:Akka (JVM): Serialize an actorref with protobuf within another message,其中提到将ActorSystem
强制转换为ExtendedActorSystem
。这是正确的方法吗?它会一直有效吗?
亲爱的@stackoverflower,
无论何时使用ActorSystem(...)
,它都会构建一个ActorSystemImpl
的实例。
类型树看起来像:
ActorSystemImpl extends ExtendedActorSystem
和
ExtendedActorSystem implements ActorSystem
您可以使用类似的语句
val system: ExtendedActorSystem = ActorSystem(...).asInstanceOf[ExtendedActorSystem]
访问正确的类型自动完成。不幸的是ActorSystemImpl
的范围是[akka]
。