2017-11-08 3 views

答えて

0

サンプルのJUnitと自動的にArchaiusを使用して...私は、XMLファイル内の任意のキーを変更したときの値が自動的に更新されている見たいのですが

サンプルコードの重要な注意:プログラムは最初のコールバックの後に終了します。

 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 
 
    <properties> 
 
     <comment>Description of the property list</comment> 
 
     <entry key="key1">value1</entry> 
 
     <entry key="key2">value2</entry> 
 
     <entry key="key3">value3</entry> 
 
    </properties> 
 

:あなたはより多くのコールバックをテストしたい場合は、クラス変数 'ラッチ'

package com.apple.paymentgateway.config; 

import java.util.HashMap; 
import java.util.Map; 
import java.util.concurrent.CountDownLatch; 

import org.apache.commons.configuration.XMLPropertiesConfiguration; 
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("key1", ""); 
     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 XMLPropertiesConfiguration("test.xml")); 
      Map<String, Object> fullProperties = new HashMap<String, Object>(); 
      configFromPropertiesFile.getProperties().forEach((k, v) -> fullProperties.put((String) k, v)); 
      return PollResult.createFull(fullProperties); 
     } 

    } 
} 

のtest.xmlでカウンターを増やします

関連する問題