简体   繁体   English

在向akka actor发送消息时发送有效载荷

[英]Send payload while sending message to an akka actor

Is there a way to add some kind of payload (attached message) while sending a message to an akka actor? 在向akka actor发送消息时,是否可以添加某种payload (附加消息)? I know there is a way to do this using routers , but can this be achieved by simple message passing? 我知道有一种使用routers执行此操作的方法,但是可以通过简单的消息传递来实现吗?

By simple message passing, I mean something like this remoteActor ! "hi" 通过简单的消息传递,我的意思是像这个remoteActor ! "hi" remoteActor ! "hi" . remoteActor ! "hi"

Yes, yes you can. 是的,可以。 You can send any payload you want but it has to be immutable (as far as i know). 您可以发送任何您想要的有效负载,但是它必须是不可变的(据我所知)。 Most of people use case class-es. 大多数人使用案例类。 For example. 例如。

case class Payload(pay: List[String], load: Int)

then you can do as follows 那么你可以做如下

actorRef ! Payload(List("string1","1gnirts"),69)

but you hve to remember to create appropriate case in receive 但您必须记得在接收时创建适当的大小写

def receive = 
   case Payload(pay,load) => doSomeStuff(load,pay)

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

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