当前位置: 动力学知识库 > 问答 > 编程问答 >

scala - Obtaining data from ActorRef in akka

问题描述:

I have the following Actor Model declaration in akka:

val wireA = system.actorOf(Props(new Wire(false)), "Input")

val wireB = system.actorOf(Props(new Wire(false)), "Output")

inverter ! generateOutput(wireA, wireB)

From the generateOutput(input:ActorRef, output:ActorRef),

I need to access the boolean parameter which constructs each particularActorRef (i.e the paramater false which is found in each respective Wire Constructor.

How does it can be reached?

网友答案:

You can send a message to each actor, asking for its current status:

case object Status
case object StatusResult(value: Boolean)

class MyActor(wire: ActorRef) extends Actor {

  wire ! Status

  def receive = {
    case StatusResult(value) => ...
  }

wireA and wireB are ActorRefs, they do not expose their state and the only way to communicate with them is via messages.

网友答案:

It's not the way you should play with Actors. You get the ActorRef and you you play with it only with the messages. You can ask the actor of the value of the boolean by passing a message to it:

wireA ! "getVal"

and waiting for the response in the sender actor. Please check the basic tutorial on Actors:

http://alvinalexander.com/scala/scala-akka-actors-ping-pong-simple-example

分享给朋友:
您可能感兴趣的文章:
随机阅读: