Scala远程演员示例 - Eclipse

时间:2014-09-16 20:56:27

标签: eclipse scala akka actor remote-actors

我使用此链接中给出的示例代码来实现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'.

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

谢谢, 凯沙夫

1 个答案:

答案 0 :(得分:2)

如果您需要等待结果(同步)以检查每件事情是否正确:

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)
}