0

我最近才开始使用 Akka 和 Scala,我正在尝试制作一个实现基本 Akka 远程处理的简单项目。

当我在没有远程处理的情况下尝试以下代码时,一切正常,但是当我将数据库参与者放在远程参与者系统上但仍在同一台机器内时,出现错误“遇到死信”

这是在 hashmap 中存储数据的数据库 actor 的代码:

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

这是客户端的代码

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"AkkaIMDB@127.0.0.1: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 _ => {}
    }

}
4

1 回答 1

0

程序中的所有内容都是正确的,除了 LocalApplication 中的演员选择应该如下:

val DBreference = system.actorSelection(s"akka.tcp://AkkaIMDB@127.0.0.1:2552/user/ImdbActor")

一旦你更新它,它应该可以工作。

永远记住,演员的 url 以协议名称开头akka.tcp://

于 2018-11-08T01:03:07.280 回答