2017-08-14 89 views
-2

我有兩個演員,例如,發件人:我要發送第二消息,演員爲新的消息

class ActorSender(actroReciever: ActorRef) extends Actor{ 
    implicit val timeout = Timeout(100, TimeUnit.SECONDS) 

    override def receive: Receive = { 
    case "RUN" => { 
     val resp = Await.result(actroReciever ? "Msg", 100.seconds) 
     println("receive response " + resp) 
    }; 
    case str:String => println(str) 
    case _ => println("Error type msg") 
    } 
} 

reciever:

class ActroReciever extends Actor{ 
    override def receive: Receive = { 
    case str:String => { 
     val snd = sender() 
     snd ! "MessageFirst" 
     snd ! "MessageSecond" 
    } 
    } 
} 

和類爲起點:

object Tester extends App { 
    val system = ActorSystem("system") 
    val receiver = system.actorOf(Props[ActroReciever](new ActroReciever()), "receiver") 
    val sender = system.actorOf(Props[ActorSender](new ActorSender(receiver)), "sender") 
    sender ! "RUN" 
} 

我想向sender()發送兩條消息,首先作爲「ask」的回覆,第二個msg作爲「new Message」,ActorSender作爲「匹配」執行,我怎麼能做到這一點?謝謝

回答

2

首先,你知道你不應該使用Await,對不對?

其次,ask?)並不意味着在演員中使用。 ask創建一個只能接收單個消息的臨時演員。這不是你的ActorSender收到答案,而是由問題創建的臨時演員。這就是爲什麼你有這種感覺,你只能發送1個答案。

你這樣做是錯誤的,你應該簡單地使用actroReciever ! "Msg"發送消息。無需在您的ActroReciever上更改任何內容。