简体   繁体   中英

Using property file values in Hazelcast XML config

Is it possible to load the property file values in hazelcast.xml .

Example : In hazelcast.xml file,

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

Loading property file using the above tag and use the property value in the hazelcast xml as below,

<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>

Is there any other way to load the property values inside the xml?

NOTE : Loaded into the application using Config cfg = new XmlConfigBuilder(xmlFileName).build();

Thanks, Harry

Harry,

Here is how you can do that

// 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

That should do the thing.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM