死信使用Akka创建消息环

时间:2017-05-04 14:08:26

标签: scala akka actor dead-letter

我试图使用远程actor创建一个示例Akka应用程序。目标是创建例如以顺序方式交换消息的16个演员(演员16与演员15,15至14等谈话,并且与演员16谈话1)。但是,我在通信方面遇到了麻烦,因为我一直有这个错误。

  

[INFO] [05/04/2017 15:45:58.248]   [ActorFlasks-akka.actor.default-调度-4]   [akka:// ActorFlasks / deadLetters]消息[java.lang.String]来自   演员[akka:// ActorFlasks / user / 16#-2022012132]来   演员[akka:// ActorFlasks / deadLetters]未送达。 [1]死了   遇到的字母。

为此,我运行应用程序的16个终端实例,始终使用不同的配置文件。我在每个实例中创建了actor系统,如下所示:

object Main extends App {

    val localId = args(0)

    val configFile = getClass.getClassLoader.getResource(s"application$localId.conf").getFile
    val config = ConfigFactory.parseFile(new File(configFile))
    val system = ActorSystem("ActorFlasks" , config)
    val remote = system.actorOf(Props[CyclonManager], name=localId)

    remote ! "START"
}

配置文件的一个例子是:

akka {
  actor {
    provider = remote
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "localhost"
      port = 50001
    }
 }
}

演员的定义如下:

class CyclonManager extends Actor {

  def propagateMessage(): Unit = {
    val localId = self.path.name.toInt
    val currentPort = 50000 + localId
    val nextHopPort = if (currentPort == 50001) 50016 else currentPort - 1
    val nextHopId = localId-1

    val nextHopRef = context.actorSelection(s"akka.tcp://ActorFlasks@localhost:$nextHopPort/user/$nextHopId")

    nextHopRef ! "NEXT"
  }

  override def receive: Receive = {
    case "START" =>
      if (self.path.name == "16") {
        propagateMessage()
      }
    case "NEXT" =>
      propagateMessage()
    case _ =>
      println("Unrecognized message")
  }
}

这是一个让我入门的简单例子,但无论我尝试什么,我都无法工作。有人知道我失败了吗?

提前谢谢你,

编辑:

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

1 个答案:

答案 0 :(得分:1)

重构并运行你的例子后,我在propagateMessage函数中发现了一个错误。

val nextHopId = localId-1

应该是

val nextHopId = if (currentPort == 50001) 16 else localId-1

如果它无法解决您的问题,请尝试运行我的快速且脏的但正在运行的代码,看看它与您的代码有什么不同:https://login.windows.net/common

在我的代码中,我只使用了演员14,15和16.您可以使用sbt "run 16"等运行每个演员。