DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Formae and PKL: Revolutionizing Infrastructure Automation
  • Pulumi: Modern Infrastructure as Code With Real Programming Languages
  • Advancing DSLs in the Age of GenAI
  • Salesforce API Integration Guide

Trending

  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • Is the Data Warehouse Dead? 3 Patterns From Enterprise Architecture That Answer This Question
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  • Observability for Agents and Workflows: Tracing Prompts, Tool Calls, and Business Outcomes End-to-End
  1. DZone
  2. Coding
  3. Languages
  4. Managing Configurations with Apache Commons Configuration

Managing Configurations with Apache Commons Configuration

Using Apache Commons Configuration to configure long-running applications.

By 
Faheem Sohail user avatar
Faheem Sohail
·
Feb. 13, 14 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
26.2K Views

Join the DZone community and get the full member experience.

Join For Free

When building long running applications you are likely to run into a number of requirements related to configurations. Lets look at what those might be.

1. Managing multiple configuration sources
2. Ability to reload configurations when they change on disk
3. Ability to persist changes to configuration files

Instead of writing your own code it is a much better idea to use Apache Commons Configuration which provides this capability.

1. Managing multiple configuration sources

Commons Configuration provides a way for you to combine multiple configuration sources such as properties files, XML configurations, system properties and database tables. This allows your application to talk to a single interface for retrieving all sort of configurations. This is done by building a composite configuration. 

In the following code snippet, we are combining configurations from a properties files as well as System properties. This of course could be extended to include XML based configurations, database table based configurations etc.

private void compositeConfigurations() throws ConfigurationException{
	CompositeConfiguration config = new CompositeConfiguration();
	config.addConfiguration(new SystemConfiguration());
	config.addConfiguration(new PropertiesConfiguration("configurations.properties"));

	System.out.println(config.getString("configuration.first"));
	System.out.println(config.getString("java.home"));
}
2. Ability to reload configurations automatically when file is changed

One obviously wants to avoid bouncing the server each time a configuration change is made therefore this is an extremely useful functionality. Properties are reloaded (based on modified date of the configuration file) whenever the file is changed on disk.

private void automaticReloadingOfConfigurations() throws Exception {
	PropertiesConfiguration configs = new  PropertiesConfiguration("configurations.properties");
	configs.setReloadingStrategy(new FileChangedReloadingStrategy());

	System.out.println(configs.getString("configuration.first"));
	Thread.sleep(10000);
	//change file on disk
	System.out.println(configs.getString("configuration.first"));	
}
3. Ability to persist changes to the configurations

Another important functionality is to be able to persist changes to the configurations back to the source of the configuration. For example, if a configuration is read from a properties file and that configuration is changed, commons config allows you to persist it back to the file. This allows you to for example, build user interfaces that change configurations that can survive server restarts.

By calling .setAutoSave(true), you can configure autosaving that persists changes to the disk as soon as you make them. If you want to have more control, you can turn autosave off and explicitly save changes by calling the save method on the configuration object.

private void autoSaving() throws Exception {
	PropertiesConfiguration configs = new PropertiesConfiguration("configurations.properties");
	configs.setAutoSave(true);
	System.out.println(configs.getString("configuration.first"));
	configs.setProperty("configuration.first", "1st");
	System.out.println(configs.getString("configuration.first"));
}
Snippet (programming) Coding best practices API integration programming langauge

Published at DZone with permission of Faheem Sohail. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Formae and PKL: Revolutionizing Infrastructure Automation
  • Pulumi: Modern Infrastructure as Code With Real Programming Languages
  • Advancing DSLs in the Age of GenAI
  • Salesforce API Integration Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook