Reloading Properties with Apache Commons Configuration

Reloading properties without restarting the server or halting the JVM is a welcome addition to your application when the properties are changed frequently. Apache Commons Configuration provides a neat way to accomplish this task with very little addition to your code pile. Here have a look …

package org.x.y.z;

import java.io.File;
import java.util.List;
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.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ProjectProperties {

	private static final Logger LOGGER = LogManager.getLogger(ProjectProperties.class);
	private static ReloadingFileBasedConfigurationBuilder builder;
	private static final File PROPERTIES_FILE = new File("/tmp/project.properties");

	static {
		Parameters parameters = new Parameters();
		builder = new ReloadingFileBasedConfigurationBuilder(PropertiesConfiguration.class)
				.configure(parameters.fileBased()
				.setListDelimiterHandler(new DefaultListDelimiterHandler(','))
				.setFile(PROPERTIES_FILE)
				// Sets the refresh delay for reloading support
				.setReloadingRefreshDelay(1000l));

		// A timer-based trigger for reloading checks
		PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(), null, 5,
				TimeUnit.SECONDS);
		trigger.start();

		builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST,
				new EventListener() {

					@Override
					public void onEvent(ConfigurationBuilderEvent event) {
						// Performs a check whether a reload operation is necessary
						builder.getReloadingController().checkForReloading(null);
					}
				});

	}

	public static String getStringValue(String key) {
		String value = null;
		try {
			value = builder.getConfiguration().getString(key);
		} catch (ConfigurationException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return value;
	}

	public static Integer getIntValue(String key) {
		Integer value = null;
		try {
			value = builder.getConfiguration().getInt(key);
		} catch (ConfigurationException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return value;
	}

	public static String[] getStringArray(String key) {
		String[] value = null;
		try {
			value = builder.getConfiguration().getStringArray(key);
		} catch (ConfigurationException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return value;
	}

	public static List getList(String key) {
		List value = null;
		try {
			value = builder.getConfiguration().getList(String.class, key);
		} catch (ConfigurationException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return value;
	}
}

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑