Bha*_*rat 5 java netflix configuration-management apache-commons-config netflix-archaius
我是 Netflix archaius 的新手。我有一个读取 Java 属性文件并打印属性值的代码片段。
当该程序运行时,它会打印 testproperty.properties 文件中名为“Fields”的属性的值。现在,当该程序运行时,我正在更新“Fields”属性的值,因此 archaius 应该动态获取更改值。但它仍在打印旧值。
在这个 Java 程序中使用 archaius 的正确方法是什么?或者在不重新启动程序的情况下更新程序中的属性?如果有人可以指出此代码片段中的更正,那将会很有帮助。
我想用Netflix archaius运行一个演示,所以我在我的项目中通过maven导入了archaius。
现在我正在更新我的属性文件。但它仍然打印旧的财产价值。(PS:我在驱动程序中保留了连续的 while 循环,以查看 archaius 是否在运行时选择更新属性值。我想这就是 archaius 应该做的。获取更新的属性而不重新启动应用程序。如果我错了,请纠正我。)
下面是我的代码片段:
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
public class PropertyChangetest {
public static void main(String args[]) {
DynamicPropertyFactory sampleProp = DynamicPropertyFactory.getInstance();
System.setProperty("archaius.configurationSource.defaultFileName", "TestProperty.properties");
System.setProperty("archaius.fixedDelayPollingScheduler.delayMills", "500");
while(true) {
DynamicStringProperty sampleProp1 = sampleProp.getStringProperty("fields","");
System.out.println(sampleProp1.get());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的“TestProperty.properties”文件只有一个名为“字段”的属性。运行程序后,我正在更新我的属性文件,但它仍然打印旧值。
这个想法是实现一个自定义的 PolledConfigurationSource,这样 Archaius 就可以轮询源并更新属性以供使用。我还包含了一个回调,这是使用该属性的智能方式,而无需您的应用程序再次轮询(请记住 Archaius 正在为您完成轮询部分)。
关于示例代码的重要说明:程序在第一次回调后退出。如果您想测试更多回调,请增加类变量“latch”处的计数器
package com.test.config;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;
import com.netflix.config.AbstractPollingScheduler;
import com.netflix.config.ConcurrentMapConfiguration;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.config.FixedDelayPollingScheduler;
import com.netflix.config.PollResult;
import com.netflix.config.PolledConfigurationSource;
public class TestArchaius {
CountDownLatch latch = new CountDownLatch(1);
@Test
public void tes() throws Exception {
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(0, 1000, false);
DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(new MyPolledConfigurationSource(), scheduler);
ConfigurationManager.install(dynamicConfiguration);
DynamicStringProperty fieldsProperty = DynamicPropertyFactory.getInstance().getStringProperty("fields", "");
fieldsProperty.addCallback(() -> {
System.out.println(fieldsProperty.get());
latch.countDown();
});
latch.await();
}
class MyPolledConfigurationSource implements PolledConfigurationSource {
@Override
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
ConcurrentMapConfiguration configFromPropertiesFile = new ConcurrentMapConfiguration(
new PropertiesConfiguration("TestProperty.properties"));
Map<String, Object> fullProperties = new HashMap<String, Object>();
configFromPropertiesFile.getProperties().forEach((k, v) -> fullProperties.put((String) k, v));
return PollResult.createFull(fullProperties);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2529 次 |
| 最近记录: |