如何在 Scala WorkSheet 和 Akka 中引用配置文件?

How do I reference a config file in Scala WorkSheet and Akka?

我试图在 Scala Worksheet 中创建一个简单的 akka 系统,但每次尝试时,我都会收到此错误。

com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(tsets.sc:148)
at com.typesafe.config.impl.SimpleConfig.findKey(tsets.sc:141)
at com.typesafe.config.impl.SimpleConfig.findOrNull(tsets.sc:168)
at com.typesafe.config.impl.SimpleConfig.find(tsets.sc:180)
at com.typesafe.config.impl.SimpleConfig.find(tsets.sc:185)
at com.typesafe.config.impl.SimpleConfig.getString(tsets.sc:242)
at akka.actor.ActorSystem$Settings.<init>(tsets.sc:166)
at akka.actor.ActorSystemImpl.<init>(tsets.sc:533)
at akka.actor.ActorSystem$.apply(tsets.sc:139)
at akka.actor.ActorSystem$.apply(tsets.sc:116)
at #worksheet#.system$lzycompute(tsets.sc:9)
at #worksheet#.system(tsets.sc:9)
at #worksheet#.get$$instance$$system(tsets.sc:9)
at A$A9$.main(tsets.sc:35)
at #worksheet#.#worksheet#(tsets.sc)

即使 Scala 工作表与 application.conf 文件位于完全相同的目录中。

文件的 akka 条目看起来像这样。

akka {
  loglevel = ??
  actor {
    provider = ??
 }
 remote {
log-remote-lifecycle-events = ??
enabled-transports =??
netty.tcp {
  hostname = ??
  port = ??
  maximum-frame-size = ??
  maximum-frame-size = ??
  send-buffer-size = ??
  send-buffer-size = ??
  receive-buffer-size = ??
  receive-buffer-size = ??
}
  }

  cluster {
roles = ["frontend"]
seed-nodes = ??
use-dispatcher = c??

failure-detector {
  threshold = ??
  acceptable-heartbeat-pause = ??
  heartbeat-interval = ??
  heartbeat-request {
    expected-response-after = ??
  }
  }
 }
}

我什至尝试将其添加到 build.sbt

assemblyMergeStrategy in assembly := {
  case PathList("application.conf") => MergeStrategy.concat
}

一种可能的变体是使用 resolve 调用 ConfigFactory.parseFile(如果您的配置文件中有一些替换,例如 receive-buffer-size = ${send-buffer-size}):

val confPath = getClass.getResource("/application.conf")
val config = ConfigFactory.parseFile(new File(confPath.getPath)).resolve()
config.getString("akka.loglevel")

您的 application.conf 文件应该在 resources 文件夹中,您的工作表可以放在 scala 文件夹中。

更新

如果你使用Intellij IDEA。这个有一个workaround

  1. 转到文件 > 设置 > 语言和框架 > Scala > 工作表
  2. 取消勾选"Run config in the compiler process"然后按"OK"

所以你可以自由使用ConfigFactory.load()

我用的IDEA版本是2018.1.5