Search code examples
eclipsescalaakkaactorremote-actors

Scala Remote Actor Example - Eclipse


I used the example code given in this link for implementing the scala remote application: https://stackoverflow.com/a/15367735/1932985

I get the following output:
Server Output:

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

Client Output:

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'.

I ran the server file first followed by the client file. Is the output I received correct? Or is the way I executed wrong? Please help me out!

Thanks, Keshav


Solution

  • If you need to wait for the results (synchronize) to check that every thing ok:

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

    the caller:

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

    If you want do use asynchronous call you need to initialize the 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)
    }