Cannot establish remote communication with Akka

142 views Asked by At

I've only started to use Akka and Scala recently and I'm trying to make a simple project that implements basic Akka remoting.

When I try the following code without remoting everything works fine however when I put the database actor on a remote actor system but still inside the same machine, I have the error "deadletters encountered"

here is the code of the database actor that stores data inside a hashmap:

class ActorDB extends Actor {

  val data = new HashMap[String, Object]
  val log = Logging(context.system, this)

  override def receive = {
      case Set(key, value) =>
        log.info("received the pair:" + key +","+ value)
        data.put(key, value)
        sender() ! Status.Success

      case Get(key) =>
      val value = data.get(key)
        value match {
          case Some(v) =>
            log.info("found the pair"+ key + "," + v)
            sender() ! v
          case None =>
            log.info("the key:" + key + ", has no corresponding value in the database")
            sender() ! Status.Failure(KeyNotFoundException(key))
      }

      case _ => Status.Failure(new ClassNotFoundException)
  }
}

object Main extends App {

  val config =  ConfigFactory.parseString(
    """
      |akka {
      |  actor {
      |    provider = "akka.remote.RemoteActorRefProvider"
      |  }
      |  remote {
      |    enabled-transports = ["akka.remote.netty.tcp"]
      |    netty.tcp {
      |      hostname = "127.0.0.1"
      |      port = 2552
      |    }
      |  }
      |}
    """.stripMargin)


  val system = ActorSystem("AkkaIMDB", ConfigFactory.load(config))
  val database = system.actorOf(Props(new ActorDB),"ImdbActor")
}

And here is the code for the client

object main extends App {

  implicit val timeout = Timeout(1 seconds)

  val config =  ConfigFactory.parseString(
    """
      |akka {
      |  actor {
      |    provider = "akka.remote.RemoteActorRefProvider"
      |  }
      |  remote {
      |    enabled-transports = ["akka.remote.netty.tcp"]
      |    netty.tcp {
      |      hostname = "127.0.0.1"
      |      port = 2553
      |    }
      |  }
      |}
    """.stripMargin)
  val system = ActorSystem("ClientSystem", ConfigFactory.load(config))
  val DBreference = system.actorSelection(s"[email protected]:2552/user/ImdbActor")

  var key: String = ""
  var value: Object = None

    println("Type S to send or R to receive a (key value) pair")
    StdIn.readLine() match {
      case "S" => {
        println("print key: ")
        key = StdIn.readLine()
        println("print value: ")
        value = StdIn.readLine()
        (DBreference ? messages.Set(key , value)).map({case akka.actor.Status.Success => println("Data added successfully")})
      }
      case "R" => {
        println("print key: ")
        key = StdIn.readLine()
        (DBreference ? messages.Get(key)).map(
          {case akka.actor.Status.Failure(KeyNotFoundException(k)) => println(s"No pair has been found with the key $k")
          case v => println(s"the key $key corresponds to the value $v")} )
      }
      case _ => {}
    }

}
1

There are 1 answers

2
user51 On BEST ANSWER

Everything in your program is correct except, the actor selection in your LocalApplication should be below:

val DBreference = system.actorSelection(s"akka.tcp://[email protected]:2552/user/ImdbActor")

Once you update this it should work.

Always remember, url of the actor starts with protocol name akka.tcp://.