【问题标题】:How to read xml file key value using netflix archaius API?如何使用 netflix archaius API 读取 xml 文件键值?
【发布时间】:2018-04-20 17:36:20
【问题描述】:

我是 Netflix Archaius 的新手。谁能提供示例代码以从 xml 文件中读取键值? 还希望看到值在我更改 XML 文件中的任何键时自动更新...

问候, 阿什什

【问题讨论】:

    标签: netflix-archaius


    【解决方案1】:

    关于如何将 xml 用于键/值对以及如何使用 Archaius 自动加载更改的 JUnit 示例:

    示例代码重要说明:程序在第一次回调后退出。如果要测试更多回调,请增加类变量 'latch' 处的计数器

    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:

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

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 2023-03-29
      • 2016-06-13
      • 2013-05-28
      • 1970-01-01
      • 2011-09-06
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多