如何编辑akka application.conf

时间:2012-05-04 09:17:50

标签: scala configuration akka

我正在寻找有关如何编写自定义application.conf的教程。我阅读了akka文档并尝试了Pi示例。

但现在我想运行自己的配置。我的目标是覆盖路由器类型并更改路由器数量。这是我的代码:

package org.sddb.playground

import akka.actor._
import com.typesafe.config.ConfigFactory
import akka.routing.RoundRobinRouter

object Test extends App {

  case object Log
  case object Ask

  class Tester extends Actor with ActorLogging {

    def receive = {
      case Log => logging
      case Ask => answer
    }

    def logging {
      log error "error"
      log warning "warning"
      log debug "debug"
    }

    def answer {
      log info "somebody asked"
    }
  }
  val config = ConfigFactory.load
  val system = ActorSystem("TestSystem", config.getConfig("test"))
  val tester = system.actorOf(Props[Tester].withRouter(RoundRobinRouter(2)))
  tester ! Log
  tester ! Ask
  tester ! PoisonPill
  system.shutdown
}

我的application.config看起来就是这样:

test {
  akka.loglevel = DEBUG
  deployment {
    /tester {
      router = broadcast
      nr-of-instances = 5
    }
  }
}

登录调试级别是正常的,但既没有广播,也没有5个实例。 我的错误在哪里?

1 个答案:

答案 0 :(得分:5)

这是因为您正在显式创建RoundRobinRouter(2)。您必须通过FromConfig(),而且还必须将“tester”作为名称传递。

编辑:

您的配置部分错误,应该是:

test {
  akka.loglevel = DEBUG
  akka.actor.deployment {
    /tester {
      router = broadcast
      nr-of-instances = 5
    }
  }
}

但你也可以这样做

akka {
  loglevel = DEBUG
  actor {
    deployment {
      /tester {
        router = broadcast
        nr-of-instances = 5
      }
    }
  }
}

然后你不必手动传递配置。