【问题标题】:How is configuration read in AKKA如何在 AKKA 中读取配置
【发布时间】:2017-01-18 20:26:08
【问题描述】:

我正在为我用 java 编写的 akka 应用程序创建自定义配置,如下所示

应用程序.conf

akka {
  actor {
    debug {
      # enable DEBUG logging of actor lifecycle changes
      lifecycle = on
    }
  }
}

我不确定如何加载此配置。从文档中不清楚这是否需要在创建 actrSystem 时显式加载,或者在运行 jar 时是否需要在类路径中。在这两种情况下,有没有我可以看的例子

这是一个maven项目,配置文件在src/main/resources下

我看到 applucation.conf 存在于 target/classes 下。是这样吗?

【问题讨论】:

标签: java maven configuration akka


【解决方案1】:

使用 ConfigFactory 您可以从 src/main/resources 加载 application.conf 文件,不一定需要 ActorSystem 来使用它

根据akka doc http://doc.akka.io/docs/akka/2.4/general/configuration.html

  • Akka 使用 Typesafe Config Library,这对于配置您自己的应用程序或使用或不使用 Akka 构建的库可能也是一个不错的选择。这个库是用 Java 实现的,没有外部依赖;你应该看看它的文档(特别是关于 ConfigFactory),它只在下面进行了总结。

一个简单的测试

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

public class ConfigFactoryTest {

    public static void main(String[] args) {
        Config conf = ConfigFactory.load("application.conf");
        System.out.println(conf.getString("akka.actor.debug.lifecycle")); //should be 'on'
     }
}

也可以使用withFallback方法 http://doc.akka.io/docs/akka/2.4/general/configuration.html#Reading_configuration_from_a_custom_location

假设您有另一个名为 fallback.conf 的属性文件

akka {
 actor {
    fallback = "fallback"
       }
}

Config fallback = ConfigFactory.load("fallback.conf")
Config conf = ConfigFactory.load("application.conf").withFallback(fallback);
        System.out.println(conf.getString("akka.actor.fallback")); //fallback

这里你可以找到例子:

https://github.com/typesafehub/config/tree/master/examples/java

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 2011-10-29
    • 2016-06-14
    相关资源
    最近更新 更多