Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在application.conf上引用.properties值_Java_Akka_Akka Remote Actor - Fatal编程技术网

Java 如何在application.conf上引用.properties值

Java 如何在application.conf上引用.properties值,java,akka,akka-remote-actor,Java,Akka,Akka Remote Actor,我有一个J2EE应用程序,它使用一个自定义的actor系统,我需要将一些自定义配置外部化 有办法做到这一点吗?由于application.conf始终在类路径上,因此我可以加载一个外部custom.properties文件并像下面那样使用它 ActorSystem.akka.remote.netty.hostname = "${custom.ip}" ActorSystem.akka.remote.netty.port = "${custom.port}" 我不完全确定您的限制条件是什么,但原

我有一个J2EE应用程序,它使用一个自定义的actor系统,我需要将一些自定义配置外部化

有办法做到这一点吗?由于
application.conf
始终在类路径上,因此我可以加载一个外部
custom.properties
文件并像下面那样使用它

ActorSystem.akka.remote.netty.hostname = "${custom.ip}"
ActorSystem.akka.remote.netty.port = "${custom.port}"

我不完全确定您的限制条件是什么,但原则上您有几种选择:

  • 创建actor系统时,您可以向其提供硬编码配置,如下所示:

    Map configMap = new HashMap();
    configMap.put("akka.remote.netty.hostname", custom.ip);
    configMap.put("akka.remote.netty.port", custom.port);
    
    Config config = ConfigFactory.parseMap(configMap).withFallback(ConfigFactory.load());
    ActorSystem system = ActorSystem.create("ActorSystem", config);
    
    include "application"
    akka.remote.netty.hostname = "custom-ip"
    akka.remote.netty.port = "custom-port"
    
    akka.remote.netty.hostname = "default-ip"
    akka.remote.netty.port = "default-port"
    akka.remote.netty.hostname = "${?custom.ip}"
    akka.remote.netty.port = "${?custom.port}"
    
  • 您可以通过代码:
    ConfigFactory.load(“custom.conf”)
    或通过设置系统属性
    -Dconfig.resource=custom.conf
    来加载自定义配置文件,而不是
    application.conf
    ,并在
    custom.conf
    中包含
    application.conf
    ,如下所示:

    Map configMap = new HashMap();
    configMap.put("akka.remote.netty.hostname", custom.ip);
    configMap.put("akka.remote.netty.port", custom.port);
    
    Config config = ConfigFactory.parseMap(configMap).withFallback(ConfigFactory.load());
    ActorSystem system = ActorSystem.create("ActorSystem", config);
    
    include "application"
    akka.remote.netty.hostname = "custom-ip"
    akka.remote.netty.port = "custom-port"
    
    akka.remote.netty.hostname = "default-ip"
    akka.remote.netty.port = "default-port"
    akka.remote.netty.hostname = "${?custom.ip}"
    akka.remote.netty.port = "${?custom.port}"
    
  • 您还可以通过系统属性提供自定义端口和ip,并使用默认值(如果未定义)。在这种情况下,
    application.conf
    如下所示:

    Map configMap = new HashMap();
    configMap.put("akka.remote.netty.hostname", custom.ip);
    configMap.put("akka.remote.netty.port", custom.port);
    
    Config config = ConfigFactory.parseMap(configMap).withFallback(ConfigFactory.load());
    ActorSystem system = ActorSystem.create("ActorSystem", config);
    
    include "application"
    akka.remote.netty.hostname = "custom-ip"
    akka.remote.netty.port = "custom-port"
    
    akka.remote.netty.hostname = "default-ip"
    akka.remote.netty.port = "default-port"
    akka.remote.netty.hostname = "${?custom.ip}"
    akka.remote.netty.port = "${?custom.port}"
    
  • 或者您可以在
    application.conf
    文件中包含
    custom.properties
    。如果
    自定义.properties
    不存在,则将忽略If
    application.conf

    akka.remote.netty.hostname = "default-ip"
    akka.remote.netty.port = "default-port"
    include "custom"
    
    custom.properties

    akka.remote.netty.hostname = "custom-ip"
    akka.remote.netty.port = "custom-port"
    

  • 谢谢,我想做的是通过java:comp/env从JNDI环境变量加载这些变量?我想在这种情况下,您可以使用备选方案1:从JNDI读取这些变量,并以编程方式将它们提供给akka系统。@VolkerStampa,您可以从system.properties自动magicaly读取属性吗?如果您想手动执行此操作,则需要知道要从System.getProperty()准备哪些属性;我想使用jar中的application.conf,并在使用akka应用程序启动jar时覆盖/添加配置。@kamiseq不太清楚您的意思。但备选方案3不是你需要的吗?您可以在jar的application.conf中指定默认值,您可以通过在命令行上定义系统属性来覆盖这些默认值。@VolkerStampa hi,谢谢您的回答。好的,所以我需要更改application.cong结构(从树到属性列表),然后如果将任何属性添加到具有-Dakka.remote.netty.hostname=other的应用程序中,akka将使用这样的“other”值而不是“default ip”?正确的?