【问题标题】:Dead letters in akka remoting (scala)akka远程处理中的死信(scala)
【发布时间】:2014-09-16 14:54:29
【问题描述】:

当我尝试在本地主机上使用远程 akka Actor 运行一个简单示例时,我遇到了死信。

这是我的远程项目的 build.sbt 文件。

name := "HelloRemote"

version := "1.0"

scalaVersion := "2.11.2"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.3.6",
  "com.typesafe.akka" %% "akka-remote" % "2.3.6"
)

这是我的远程系统的 application.conf 文件。

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
   }
   remote {
     enabled-transports = ["akka.remote.netty.tcp"]
     netty.tcp {
       hostname = "127.0.0.1"
       port = 5100
     }
   }
}

这是我的远程系统的 HelloRemote.scala 文件。

package remote

import akka.actor._

object HelloRemote extends App  {
  val system = ActorSystem("HelloRemoteSystem")
  val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
  remoteActor ! "The RemoteActor is alive"
}

class RemoteActor extends Actor {
  def receive = {
    case msg: String =>
        println(s"RemoteActor received message '$msg'")
        sender ! "Hello from the RemoteActor"
  }
}

对于我的本地系统,build.sbt 文件如下。

name := "HelloLocal"

version := "1.0"

scalaVersion := "2.11.2"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.3.6",
  "com.typesafe.akka" %% "akka-remote" % "2.3.6"
)

我本地系统的 application.conf 文件是

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 0
    }
  }
}

我本地系统的 HelloLocal.scala 文件是

package local

import akka.actor._

object Local extends App {

  val system = ActorSystem("LocalSystem")
  val localActor = system.actorOf(Props[LocalActor], name = "LocalActor")  // the local actor
  localActor ! "START"                                                     // start the action

}

class LocalActor extends Actor {

  // create the remote actor
  val remote = context.actorSelection("akka.tcp://HelloRemoteSystem@127.0.0.1:5100/user/RemoteActor")
  var counter = 0

  def receive = {
    case "START" =>
        remote ! "Hello from the LocalActor"
    case msg: String =>
        println(s"LocalActor received message: '$msg'")
        if (counter < 5) {
            sender ! "Hello back to you"
            counter += 1
        }
  }
}

当我第一次运行 HelloRemote.scala The RemoteActor is alive 按预期打印然后我立即收到错误

[INFO] [09/16/2014 10:52:47.585] [HelloRemoteSystem-akka.actor.default-dispatche
r-4] [akka://HelloRemoteSystem/deadLetters] Message [java.lang.String] from Acto
r[akka://HelloRemoteSystem/user/RemoteActor#1051175275] to Actor[akka://HelloRem
oteSystem/deadLetters] was not delivered. [1] dead letters encountered. This log
ging can be turned off or adjusted with configuration settings 'akka.log-dead-le
tters' and 'akka.log-dead-letters-during-shutdown'.

我在运行本地系统 HelloLocal.scala 时遇到类似的错误,然后什么也没有发生。我在这里做错了吗?

【问题讨论】:

    标签: scala akka remoting


    【解决方案1】:

    当你从一个actor外部发送消息时,Akka会填写死信邮箱作为发件人。当您在HelloRemote.scala 中的RemoteActor 回复时,它正在回复死信邮箱,因为它收到的消息是在演员之外发送的。

    目前,HelloRemote.scala 甚至不涉及远程处理,因为您只是部署本地参与者。

    当您运行 HelloLocal.scala 时,我怀疑您的 HelloRemote 演员系统正在终止,因为没有什么可以保持该 main 运行。

    【讨论】:

    • 好的,我明白你在说什么,但我仍然不确定我必须对我的代码进行哪些更改才能使其正常工作。我是否应该在 HelloRemote.scala 中删除我从演员外部发送消息的部分
    • 死信来自对消息“The RemoteActor is alive”的回复,因为没有发件人,正如 Ryan 指出的那样。除此之外,您的示例正在工作!
    【解决方案2】:

    我之前也遇到过同样的问题,我通过使用val config = ConfigFactory.parsingString("---Configuration---")而不是使用配置文件来解决它。 希望它对你有用。

    【讨论】:

    • 谢谢;经过一天的努力,想知道为什么在 java 控制台中运行时会出现这个死信,尝试了这个,它奏效了。阿门 StackOverflow
    【解决方案3】:

    我也遇到了这个问题,然后做了同样的例子。我错过的部分是将 application.conf 放在 src/main/resources 目录中。

    如果在启动服务器时它位于正确的位置,您应该会看到类似这样的 INFO 消息:

    [INFO] [03/02/2016 19:06:10.365] [main] [akka.remote.Remoting] Starting remoting
    [INFO] [03/02/2016 19:06:10.497] [main] [akka.remote.Remoting] Remoting started; listening on addresses : akka.tcp://HelloRemoteSystem@127.0.0.1:5100]
    [INFO] [03/02/2016 19:06:10.499] [main] [akka.remote.Remoting] Remoting now listens on addresses: [akka.tcp://HelloRemoteSystem@127.0.0.1:5100]
    

    blog post 我关注的目录结构:

    HelloRemote/
    |-- build.sbt
    |-- src
        |-- main
        │   |-- java
        │   |-- resources
        │   │   +-- application.conf <--------------------------
        │   +-- scala
        │       +-- remote
        │           +-- HelloRemote.scala
        +-- test
            |-- java
            |-- resources
            +-- scala
    

    正如Ryan 已经提到的,死信错误是字符串消息“The RemoteActor is alive”不是演员的副作用,因此它无法接收回消息“Hello from the RemoteActor”。这是示例的一个小错误,但不会导致远程和本地参与者无法相互交谈。

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      相关资源
      最近更新 更多