如何从sbt shell向actor发送消息

时间:2017-02-11 16:58:07

标签: scala sbt akka akka-actor

我创建了一个带有远程actor的简单app(来自here的例子):

object HelloRemote extends App  {
  val system = ActorSystem("HelloRemoteSystem")
  val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
  remoteActor ! "The RemoteActor is alive"
}

class RemoteActor extends Actor {
  def receive = {
    case msg: String =>
        println(s"RemoteActor received message '$msg'")
        sender ! "Hello from the RemoteActor"
  }
}

是否可以从sbt shell发送消息?

2 个答案:

答案 0 :(得分:0)

只有Actor才能向另一个Actor发送消息。

答案 1 :(得分:0)

只需要一个actor引用即可向Actor发送消息。例如您可以在scala shell中执行相同的操作: 请遵循以下:

import akka.actor._

在shell中定义你的Actor。

class RemoteActor extends Actor {
        def receive = {
          case msg: String =>
              println(s"RemoteActor received message '$msg'")
              sender ! "Hello from the RemoteActor"
        }
        }

val system = ActorSystem("HelloRemoteSystem")
val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
remoteActor ! "The RemoteActor is alive"

这里remoteActor是实例化Actor的引用。你可以从任何地方发送消息,如果1.这是演员活着的2.你可以在那里找到一个演员参考。