Scala远程演员示例 - Eclipse

我使用此链接中给出的示例代码来实现scala远程应用程序:https://stackoverflow.com/a/15367735/1932985

我得到以下输出:
服务器输出:

akka://GreetingSystem/user/joe
Server ready
joe received local msg! from Actor[akka://GreetingSystem/deadLetters]

客户输出:

STARTING
That 's Joe:ActorSelection[Anchor(akka://GreetingSystem-1/deadLetters), Path(/user/joe)]
Client has sent Hello to joe
[INFO] [09/16/2014 16:39:49.167] [GreetingSystem-1-akka.actor.default-dispatcher-5]   [akka://GreetingSystem-1/deadLetters] Message [java.lang.String] from Actor[akka://GreetingSystem-1/deadLetters] to Actor[akka://GreetingSystem-1/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/16/2014 16:39:49.168] [GreetingSystem-1-akka.actor.default-dispatcher-5] [akka://GreetingSystem-1/deadLetters] Message [java.lang.String] from Actor[akka://GreetingSystem-1/user/$a#-555317575] to Actor[akka://GreetingSystem-1/deadLetters] was not delivered. [2] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

我首先运行服务器文件,然后运行客户端文件 . 我收到的输出是否正确?或者我执行错误的方式是什么?请帮帮我!

谢谢,Keshav

回答(1)

2 years ago

如果你需要等待结果(同步)来检查每件事情是否正常:

case class startMsg(id: Int)
class ActorExamp extends Actor {
    org.slf4j.LoggerFactory.getILoggerFactory.getLogger(this.getClass.getName);
    var logger = org.slf4j.LoggerFactory.getILoggerFactory.getLogger(this.getClass.getName);

  def receive = {
    case startMsg(id) => {
      val caller=sender // save the sender so you always call the right one
      logger.info("Actor is starting")
      println("Actor is starting, id: "+id)
      caller ! "OK"
  }
 }
}

呼叫者,召集者:

implicit val timeoutActor = akka.util.Timeout(12, TimeUnit.HOURS)
object test extends App{
    implicit val timeoutActor = akka.util.Timeout(12, TimeUnit.HOURS)
    val syncActor = ActorSystem().actorOf(Props[ProcessesInf])
    val ansActor=syncActor ? startMsg(22)
    val res2= Await.result(ansActor, timeoutActor.duration).asInstanceOf[String]
    println(res2.toString)
}

如果你想使用异步调用,你需要初始化actor:

object test extends App{
    val system = ActorSystem("akka")
    val asyncActor = system.actorOf(Props(new ProcessesInf), "ProcessesInf")
    val res1=asyncActor  ! startMsg(22)
    println(res1.toString)
}