gpt4 book ai didi

scala - Akka Actor : Remote actor exception "Futures timed out after"

转载 作者:行者123 更新时间:2023-12-04 06:27:13 24 4
gpt4 key购买 nike

我是 Akka 新手,我使用 Akka 2.3.3 版本来创建 actor。我将创建远程参与者并尝试使用客户端进行访问。每当我要运行测试用例时,都会抛出以下异常:

[INFO] [04/27/2016 07:51:23.727] [Localsystem-akka.actor.default-dispatcher-3] [akka://Localsystem/deadLetters] Message [com.harmeetsingh13.chapter2.messages.SetRequest] from Actor[akka://Localsystem/temp/$a] to Actor[akka://Localsystem/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] [04/27/2016 07:51:23.745] [Localsystem-akka.actor.default-dispatcher-3] [akka://Localsystem/deadLetters] Message [com.harmeetsingh13.chapter2.messages.GetRequest] from Actor[akka://Localsystem/temp/$b] to Actor[akka://Localsystem/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'.

Futures timed out after [10 seconds]
java.util.concurrent.TimeoutException: Futures timed out after [10 seconds]
at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:219)
at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:223)
at scala.concurrent.Await$$anonfun$result$1.apply(package.scala:190)
at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
at scala.concurrent.Await$.result(package.scala:190)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(SClientIntegrationSpec.scala:18)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(SClientIntegrationSpec.scala:15)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(SClientIntegrationSpec.scala:15)
at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSpecLike$$anon$1.apply(FunSpecLike.scala:422)
at org.scalatest.Suite$class.withFixture(Suite.scala:1122)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec.withFixture(SClientIntegrationSpec.scala:11)
at org.scalatest.FunSpecLike$class.invokeWithFixture$1(FunSpecLike.scala:419)
at org.scalatest.FunSpecLike$$anonfun$runTest$1.apply(FunSpecLike.scala:431)
at org.scalatest.FunSpecLike$$anonfun$runTest$1.apply(FunSpecLike.scala:431)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
at org.scalatest.FunSpecLike$class.runTest(FunSpecLike.scala:431)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec.runTest(SClientIntegrationSpec.scala:11)
at org.scalatest.FunSpecLike$$anonfun$runTests$1.apply(FunSpecLike.scala:464)
at org.scalatest.FunSpecLike$$anonfun$runTests$1.apply(FunSpecLike.scala:464)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:413)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:401)
............

我的服务器代码如下:Main.scala

object Main extends App{


private val configFile = getClass.getClassLoader.getResource("application.conf").getFile;
private val config = ConfigFactory.parseFile(new File(configFile ))

val system = ActorSystem("SimpleClientServer", config)
system.actorOf(Props[AkkadmeyDB], name = "akkademy-db")
}

应用程序.conf:

akka{
actor{
provider = "akka.remote.RemoteActorRefProvider"
}
remote{
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 2552
}
log-sent-messages = on
log-received-messages = on
}
}

AkkadmeyDB.scala Actor 类:

class AkkadmeyDB extends Actor{

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

override def receive: Receive = {
case SetRequest(key, value) =>
log.info("received SetRequest - key: {} value: {}", key, value)
map.put(key, value)
sender() ! Status.Success
case GetRequest(key) =>
log.info("received GetRequest - key: {}", key)
val response = map.get(key)
response match{
case Some(x) => sender() ! x
case None => Status.Failure(new KeyNotFoundException(key))
}
case o => Status.Failure(new ClassNotFoundException())
}
}

客户端代码如下:SClient.scala

class SClient(remoteIp: String) {

private implicit val timeout = Timeout(10 seconds)
private implicit val system = ActorSystem("Localsystem")
private val remoteAddress = s"akka.tcp://SimpleClientServer@$remoteIp/user/akkademy-db";
private val remoteDb = system.actorSelection(remoteAddress)

def set(key: String, value: Object) = {
remoteDb ? SetRequest(key, value)
}

def get(key: String) = {
remoteDb ? GetRequest(key)
}
}

SClientIntegrationSpec.scala 测试用例:

class SClientIntegrationSpec extends FunSpecLike with Matchers {

val client = new SClient("127.0.0.1:2552")
describe("akkadment-db-client"){
it("should set a value"){
client.set("jame", new Integer(1313))
val futureResult = client.get("james")
val result = Await.result(futureResult, 10 seconds)
result should equal (1313)
}
}
}

当我看到远程应用程序的日志时,似乎请求命中没有发送到服务器。我的示例代码运行有什么问题?

最佳答案

为了解决上述问题,我们需要遵循以下两个步骤:

  1. 当我创建服务器代码时,我从服务器应用程序中排除 application.conf,这就是客户端应用程序无法与服务器连接的原因。 built.sbt中使用的代码如下:

    mappings in (Compile, packageBin) ~= { _.filterNot { case (_, name) =>
    Seq("application.conf").contains(name)
    }}

注释掉上面的代码后,客户端成功看到服务端。

  • 学习Scala 第2章 jasongoodwin中解释了客户端和服务器Actor系统的代码。但书中有一些勘误表,并且缺少客户端的 application.conf 配置。因为当我们在同一台 PC 上运行这两个代码时,我们已经面临端口绑定(bind)异常,因为默认情况下参与者使用 2552 端口进行访问,并且我们已经为我们的服务器应用程序定义了该端口。因此,application.conf 还需要客户端,如下所示:

    akka {
    actor {
    provider = "akka.remote.RemoteActorRefProvider"
    }
    remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
    hostname = "127.0.0.1"
    port = 0
    }
    log-sent-messages = on
    log-received-messages = on
    }
    }
  • 这里端口0表示任何空闲端口。

    之后,上面的代码就成功运行了。

    关于scala - Akka Actor : Remote actor exception "Futures timed out after",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879286/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com