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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Writing a Vector Database in a Week in Rust
  • Structured Logging
  • Microservices With Apache Camel and Quarkus (Part 2)
  • Health Check Response Format for HTTP APIs

Trending

  • Writing a Vector Database in a Week in Rust
  • Structured Logging
  • Microservices With Apache Camel and Quarkus (Part 2)
  • Health Check Response Format for HTTP APIs

Managing Configurations with Apache Commons Configuration

Faheem Sohail user avatar by
Faheem Sohail
·
Feb. 13, 14 · Interview
Like (2)
Save
Tweet
Share
24.72K 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"));
}

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

Opinions expressed by DZone contributors are their own.

Trending

  • Writing a Vector Database in a Week in Rust
  • Structured Logging
  • Microservices With Apache Camel and Quarkus (Part 2)
  • Health Check Response Format for HTTP APIs

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: