gpt4 book ai didi

java - Akka 配置异常 $WrongType : myconf has type STRING rather than OBJECT

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:48 27 4
gpt4 key购买 nike

我正在尝试读取 Akka 中的外部 conf/属性文件。我指的是这个 post

我只有一个配置,我需要在 myappConfiguration.properties 中添加更多属性。我可以看到属性文件能够加载但无法读取配置。

myappConfiguration.properties内容如下。

myconf {
directory : "D:\\Git\\Java_Test_WS\\file.reader\\"
}

我收到以下异常,异常清楚地说明了一些类型问题,但我不明白我应该写什么。

Exception in thread "main" com.typesafe.config.ConfigException$WrongType: myappConfiguration.properties: myconf has type STRING rather than OBJECT
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:159)
at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:145)
at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:172)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:184)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:189)
at com.typesafe.config.impl.SimpleConfig.getObject(SimpleConfig.java:258)
at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:264)
at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:37)
at com.myapp.file.reader.AkkaBaseEventReader.main(AkkaBaseEventReader.java:27)

下面是我试图读取该配置条目的 Java 代码。我在第 27 行的代码中遇到异常:

Config config = system.settings().config().getConfig("myconf.directory");

完整代码:

public class AkkaBaseEventReader implements ApplicationConstant {

public static void main(String[] args) throws IOException {

System.setProperty("config.file","myappConfiguration.properties");

ActorSystem system = ActorSystemImpl.create("FileReaderForassignment");
final ActorRef reader = system.actorOf(EventScannerActor.props(),"FileReader");

System.out.println(system.settings());

Config config = system.settings().config().getConfig("myconf.directory");

}
}

下面是我的 pom.xml,它将显示我正在使用的 Akka 版本:

<properties>
<akka.version>2.4.9</akka.version>
<maven-dependency-plugin.version>3.0.0</maven-dependency-plugin.version>
<maven.compiler.plugin>3.6.1</maven.compiler.plugin>
<java.compiler.target>1.8</java.compiler.target>
<java.compiler.source>1.8</java.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-core_2.11</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-experimental_2.11</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-jackson-experimental_2.11</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<!-- This will download source so easy to see API and java doc. -->
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Java 8 compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin}</version>
<configuration>
<source>${java.compiler.source}</source>
<target>${java.compiler.target}</target>
</configuration>
</plugin>

</plugins>
</build>
</project>

在此先感谢您提供的任何帮助和信息。

最佳答案

根据您的配置,directory 的值是一个 String 而不是另一个 Config 对象:

myconf {
directory : "D:\\Git\\Java_Test_WS\\file.reader\\"
}

要获取 String 的值,您应该这样做:

Config config = system.settings().config().getString("myconf.directory");

关于java - Akka 配置异常 $WrongType : myconf has type STRING rather than OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42882371/

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