3

我對Netflix archaius非常陌生。我有一個讀取Java屬性文件並打印屬性值的代碼片段。Netflix archaius無法讀取更新的屬性文件值

當此程序運行時,它將打印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()); 
     } 
    } 
} 

我「TestProperty.properties」文件只能有一個屬性稱爲字段。運行該程序後,我正在更新我的屬性文件,但它仍然打印較舊的值。

+0

請給我們一個具體的問題。 – aribeiro

+0

當此程序運行時,它將打印testproperty.properties文件中名爲「Fields」的屬性的值。現在,當這個程序運行時,我正在更新「Fields」屬性的值,所以archaius應該動態獲取更改值。但它仍然在打印較舊的值。 – Bharat

+0

好吧,我知道你有問題。但你仍然沒有問任何問題。你介意用你的疑惑更新你的文章嗎? – aribeiro

回答

0

這個想法是實現一個自定義的PolledConfigurationSource,所以Archaius可以輪詢源和更新屬性的消費。我還包括一個回調,即在沒有應用程序再次輪詢它的情況下消費財產的巧妙方法(請記住,Archaius正在爲您執行投票部分)。

有關示例代碼的重要說明:程序在第一次回調後退出。如果你想測試更多的回調,增加計數器在類變量'鎖'

package com.apple.paymentgateway.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); 
     } 

    } 
}