公用配置2 ReloadingFileBasedConfiguration [英] Commons Configuration2 ReloadingFileBasedConfiguration

查看:108
本文介绍了公用配置2 ReloadingFileBasedConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的代码库中实现Apache Configuration 2

I am trying to implement the Apache Configuration 2 in my codebase

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.event.EventListener;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;

import org.apache.commons.configuration2.CompositeConfiguration;

public class Test {

    private static final long DELAY_MILLIS = 10 * 60 * 5;


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
        PropertiesConfiguration props = null;
        try {
            props = initPropertiesConfiguration(new File("/tmp/DEV.properties"));
        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        compositeConfiguration.addConfiguration( props );
        compositeConfiguration.addEventListener(ConfigurationBuilderEvent.ANY,
                new EventListener<ConfigurationBuilderEvent>()
                {
                    @Override
                    public void onEvent(ConfigurationBuilderEvent event)
                    {
                        System.out.println("Event:" + event);

                    }
                });

        System.out.println(compositeConfiguration.getString("property1"));

        try {
            Thread.sleep(14*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Have a script which changes the value of property1 in DEV.properties
        System.out.println(compositeConfiguration.getString("property1"));
    }

    protected static PropertiesConfiguration initPropertiesConfiguration(File propsFile) throws ConfigurationException { 

        if(propsFile.exists()) {

            final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
                    new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
                    .configure(new Parameters().fileBased()
                        .setFile(propsFile)
                        .setReloadingRefreshDelay(DELAY_MILLIS)
                        .setThrowExceptionOnMissing(false)
                        .setListDelimiterHandler(new DefaultListDelimiterHandler(';')));
            final PropertiesConfiguration propsConfiguration = builder.getConfiguration();
            PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(),
                    null, 1, TimeUnit.SECONDS);
                trigger.start();

            return propsConfiguration;
        } else {
            return new PropertiesConfiguration();
        }

    }
}

这是我用来检查自动重新加载是否有效的示例代码.但是,当基础属性文件更新时,配置不会反映出来.

Here is a sample code that I using to check whether the Automatic Reloading works or not. However when the underlying property file is updated, the configuration doesn't reflect it.

推荐答案

根据文档:

使用这种重新加载方法时要记住的重要一点是,仅当将构建器用作访问配置数据的中心组件时,重新加载才起作用.从构建器获得的配置实例不会自动更改!因此,如果应用程序在启动时从构建器中获取配置对象,然后在整个生命周期内使用它,则外部配置文件上的更改将永远变得不可见.正确的方法是集中引用构建器,并在每次需要配置数据时从那里获取配置.

One important point to keep in mind when using this approach to reloading is that reloads are only functional if the builder is used as central component for accessing configuration data. The configuration instance obtained from the builder will not change automagically! So if an application fetches a configuration object from the builder at startup and then uses it throughout its life time, changes on the external configuration file become never visible. The correct approach is to keep a reference to the builder centrally and obtain the configuration from there every time configuration data is needed.

https://commons.apache.org/正确/commons-configuration/userguide/howto_reloading.html#Reloading_File-based_Configurations

这与以前的实现不同.

通过进行两项更改,我能够成功执行您的示例代码:

I was able to successfully execute your sample code by making 2 changes :

  1. 使构建器全局可用,并从构建器访问配置:

  1. make the builder available globally and access the configuration from the builder :

System.out.println(builder.getConfiguration().getString("property1"));

将侦听器添加到构建器中:`builder.addEventListener(ConfigurationBuilderEvent.ANY,新的EventListener(){

add the listener to the builder : `builder.addEventListener(ConfigurationBuilderEvent.ANY, new EventListener() {

    public void onEvent(ConfigurationBuilderEvent event) {
        System.out.println("Event:" + event);
    }
});

发布示例程序,在那里我能够成功地演示它

Posting my sample program, where I was able to successfully demonstrate it

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.event.EventListener;
import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;

public class TestDynamicProps {
    public static void main(String[] args) throws Exception {

        Parameters params = new Parameters();
        ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
            .configure(params.fileBased()
                .setFile(new File("src/main/resources/override.properties")));
        PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(),
            null, 1, TimeUnit.SECONDS);
        trigger.start();

        builder.addEventListener(ConfigurationBuilderEvent.ANY, new EventListener<ConfigurationBuilderEvent>() {

            public void onEvent(ConfigurationBuilderEvent event) {
                System.out.println("Event:" + event);
            }
        });

        while (true) {
            Thread.sleep(1000);
            System.out.println(builder.getConfiguration().getString("property1"));
        }

    }
}

这篇关于公用配置2 ReloadingFileBasedConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆