在 Hazelcast XML 配置中使用 属性 文件值
Using property file values in Hazelcast XML config
是否可以在 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 中加载 属性 值?
注意: 加载到应用程序中使用
Config cfg = new XmlConfigBuilder(xmlFileName).build();
谢谢,
哈利
哈利,
这是您可以做到的方法
// 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
这应该可以做到。
谢谢
是否可以在 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 中加载 属性 值?
注意: 加载到应用程序中使用 Config cfg = new XmlConfigBuilder(xmlFileName).build();
谢谢, 哈利
哈利,
这是您可以做到的方法
// 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
这应该可以做到。
谢谢