简体   繁体   English

将Akka actorRef发送给json

[英]Sending an Akka actorRef to json

Ok so i am writing implicit conversions for case classes in scala, using SJSON, to send messages to remote actors using the akka framework. 好的,我正在使用SJSON为scala中的case类编写隐式转换,以使用akka框架向远程actor发送消息。 One of the case classes looks like this 其中一个案例类看起来像这样

case class Example(id: String, actr: ActorRef) 

How would i go about writing the implicit for this case class. 我将如何编写此案例类的隐式。

I have seen that ActorRefs do have a toBinary method but i need to send it toJson 我已经看到ActorRef确实有一个toBinary方法,但我需要将它发送到json

  • http://doc.akkasource.org/serialization-scala . http://doc.akkasource.org/serialization-scala Explicit [deep] serialization may be required only for stateful actors, when underlying actor instance (under ActorRef / RemoteActorRef) holds some important runtime data. 当底层actor实例(在ActorRef / RemoteActorRef下)保存一些重要的运行时数据时,可能只需要对有状态actor进行显式[深度]序列化。 For this case, you should implement the following typeclass for your actor: 对于这种情况,您应该为您的actor实现以下类型类:
 /** * Type class definition for Actor Serialization */ trait FromBinary[T <: Actor] { def fromBinary(bytes: Array[Byte], act: T): T } trait ToBinary[T <: Actor] { def toBinary(t: T): Array[Byte] } // client needs to implement Format[] for the respective actor trait Format[T <: Actor] extends FromBinary[T] with ToBinary[T] 

If you want ScalaJSON serialization, instead of the default one, you should use SerializerBasedActorFormat trait 如果您想要ScalaJSON序列化而不是默认序列化,则应使用SerializerBasedActorFormat特征

trait SerializerBasedActorFormat[T <: Actor] extends Format[T] {
  val serializer: Serializer
  def fromBinary(bytes: Array[Byte], act: T) = serializer.fromBinary(bytes, Some(act.self.actorClass)).asInstanceOf[T]
  def toBinary(ac: T) = serializer.toBinary(ac)
}

with ScalaJSON serializer . 使用ScalaJSON serializer SJSON library supports serialization of plain Scala objects out-of-box, without an additional configuration (which is enough, in the most cases). SJSON库支持开箱即用的普通Scala对象的序列化,无需额外配置(在大多数情况下这已足够)。 If you need to ignore some properties, or define serialization policy of embedded objects, read this . 如果您需要忽略某些属性或定义嵌入对象的序列化策略,请阅读此内容

In your case, you would need something like 在你的情况下,你需要类似的东西

@BeanInfo
case class Example(id: String, 
@(JSONTypeHint @field)(value = classOf[MyActor])
actr: ActorRef) 

implicit object MyActorFormat extends SerializerBasedActorFormat[MyActor] {
    val serializer = Serializer.ScalaJSON
}
  • In general, you don't need to serialize your case classes explicitly, when you're sending messages to remote actors in Akka - Akka itself serializes all data with protobufs before sending over TCP. 通常,当你向Akka中的远程actor发送消息时,你不需要显式地序列化你的case类--Akka本身在通过TCP发送之前用protobufs序列化所有数据。
  • Why would you need serialize reference to the actor? 为什么需要序列化对actor的引用? If it's just needed to call the sender by the actor that receives the message, you can simply use self.sender , if the message was sent with ! 如果只需要接收消息的actor调用发送者,你可以简单地使用self.sender ,如果消息是与!一起发送的! , or self.senderFuture , when the messages is sent with !! ,或者self.senderFuture ,当邮件发送!! or !!! 还是!!! . ActorRef (or RemoteActorRef) on itself is just an abstract interface to an actor, used to encapsulate internal actor's implementation and letting externals communicate with the actor only via messages (in contrast to stdlib Actors / much like it's done in Erlang [processes]) and holds very small amount of data that makes sense to serialize and send over wire. ActorRef(或RemoteActorRef)本身只是一个actor的抽象接口,用于封装内部actor的实现,并让外部只通过消息与actor通信(与stdlib Actors相比,就像它在Erlang [processes]中完成的那样)和保存非常少量的数据,这些数据对于序列化和通过线路发送是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM