【问题标题】:Using property file values in Hazelcast XML config在 Hazelcast XML 配置中使用属性文件值
【发布时间】:2016-07-19 13:31:01
【问题描述】:

是否可以加载hazelcast.xml中的属性文件值。

示例: 在hazelcast.xml 文件中,

<context:property-placeholder location="/home/local/Documents/testproperty/test.properties"/>

使用上述标签加载属性文件,并使用 hazelcast xml 中的属性值,如下所示,

<properties>
<property name="hazelcast.max.no.heartbeat.seconds" value = "${HAZELCAST_MAX_NO_HEARTBEAT_SECONDS}"></property>
<property name="hazelcast.client.heartbeat.timeout" value = "${HAZELCAST_CLIENT_HEARTBEAT_TIMEOUT}"></property>

还有其他方法可以加载xml里面的属性值吗?

注意:使用加载到应用程序中 配置cfg = new XmlConfigBuilder(xmlFileName).build();

谢谢, 哈利

【问题讨论】:

标签: java xml properties hazelcast


【解决方案1】:

哈利,

你可以这样做

// our you can inject this using Spring   
Properties properties = new Properties();
properties.load(CurrentClass.class.getClassLoader().getResourceAsStream("hazelcast.properties"));

final Config config = new XmlConfigBuilder("hazelcast-with-properties.xml")
        .setProperties(properties) // this is how you can set the properties 
        .build();

final HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);

hazelcast.xml

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.6.xsd"
       xmlns="http://www.hazelcast.com/schema/config">
   <properties>
     <!-- pay attention to the format - property tag doesn't have value attribute -->
     <property name="hazelcast.max.no.heartbeat.seconds">${HAZELCAST_MAX_NO_HEARTBEAT_SECONDS}</property>
     <property name="hazelcast.client.heartbeat.timeout">${HAZELCAST_CLIENT_HEARTBEAT_TIMEOUT}</property>
     <property name="hazelcast.backpressure.enabled">${HAZELCAST_BACKPRESSURE_ENABLED}</property>
  </properties>
  <group>
      <name>${group.name}</name>
      <password>${group.password}</password>
  </group>
</hazelcast>

hazelcast.properties

group.name=devFromProp
group.password=supA$ecret42
HAZELCAST_MAX_NO_HEARTBEAT_SECONDS=5
HAZELCAST_CLIENT_HEARTBEAT_TIMEOUT=500
HAZELCAST_BACKPRESSURE_ENABLED=true

这应该做的事情。

谢谢

【讨论】:

  • 即使我们尝试过使用 FileSystemConfig,属性也一样,它的工作原理就像一只苍蝇。我们有相同的客户端配置选项吗?
  • 我现在没有客户的 Java 文档,但是我很确定您可以使用 Java 客户端 XML 配置进行同样的操作
猜你喜欢
  • 2020-03-05
  • 1970-01-01
  • 2017-05-27
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 2013-07-02
相关资源
最近更新 更多