Skip to content

Instantly share code, notes, and snippets.

@paulwellnerbou
Last active October 8, 2018 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulwellnerbou/3e309535a8a516477dc2bede478f79f2 to your computer and use it in GitHub Desktop.
Save paulwellnerbou/3e309535a8a516477dc2bede478f79f2 to your computer and use it in GitHub Desktop.
Code Examples Apache Commons Configuration2 Documentation
/*
* First example from https://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html#Reloading_File-based_Configurations
*/
Parameters params = new Parameters();
// Read data from this file
File propertiesFile = new File("config.properties");
ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder =
new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
.configure(params.fileBased()
.setFile(propertiesFile));
PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(),
null, 1, TimeUnit.MINUTES);
trigger.start();
/*
* Second example from https://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html#Reloading_File-based_Configurations
*/
// Create and initialize the builder
final ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder =
new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
.configure(...);
// Register an event listener for triggering reloading checks
builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST,
new EventListener()
{
@Override
public void onEvent(Event event)
{
builder.getReloadingController().checkForReloading(null);
}
});
}
@sameer1sharma
Copy link

Hi,
I have tried with same example with builder.getReloadingController().checkForReloading(null); in my event trigger but still file is not reloading in memory. Get same property value after reloading.

try{

		FileBasedConfigurationBuilder<org.apache.commons.configuration2.PropertiesConfiguration> builder =
			    new FileBasedConfigurationBuilder<org.apache.commons.configuration2.PropertiesConfiguration>(org.apache.commons.configuration2.PropertiesConfiguration.class)
			    .configure(new Parameters().properties()
			        .setFileName(folderPathUserSetting+"\\"+HarmonyScreenCaptureConstant.SCREENCAPTURE_USERSETTING_PROPERTIES_FILE)
			        .setThrowExceptionOnMissing(true)
			        .setListDelimiterHandler(new DefaultListDelimiterHandler('\n'))
			        .setIncludesAllowed(false));
			org.apache.commons.configuration2.PropertiesConfiguration config = builder.getConfiguration();
		
		// Create a file handler and associate it with the configuration
		FileHandler handler = new FileHandler(config);
		// Load another configuration source, for instance from a relative path
		handler.load(folderPathUserSetting+"\\"+HarmonyScreenCaptureConstant.SCREENCAPTURE_USERSETTING_PROPERTIES_FILE);
		
		//PropertiesConfiguration config1 = new PropertiesConfiguration(folderPathUserSetting+"\\"+HarmonyScreenCaptureConstant.SCREENCAPTURE_USERSETTING_PROPERTIES_FILE);
		System.out.println("Before reload: "+userSettingProperties.getProperty("isHighlightInteractedObject"));
		if(stateId == 1){// Checked
			System.out.println("checked.....");
			config.setProperty("isHighlightInteractedObject", "true");
		 }else if(stateId == 2){ // Unchecked
			 System.out.println("Unchecked.....");
			 config.setProperty("isHighlightInteractedObject", "false");
		 }
		// Store the resulting configuguration in a new file
		File out = new File(folderPathUserSetting+"\\"+HarmonyScreenCaptureConstant.SCREENCAPTURE_USERSETTING_PROPERTIES_FILE);
		handler.save(out);
		//config.save();
		//Thread.sleep(5000);
		//config.setReloadingStrategy(new FileChangedReloadingStrategy());
		
		Parameters params = new Parameters();
		// Read data from this file
		File propertiesFile = new File(folderPathUserSetting+"\\"+HarmonyScreenCaptureConstant.SCREENCAPTURE_USERSETTING_PROPERTIES_FILE);

		ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builderREload =
		    new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(org.apache.commons.configuration2.PropertiesConfiguration.class)
		    .configure(params.fileBased()
		        .setFile(propertiesFile));
		
		builderREload.getReloadingController();
		System.out.println("After reload: "+userSettingProperties.getProperty("isHighlightInteractedObject"));
		System.out.println("Config Property Successfully Updated..");
	}catch(Exception e){
		e.printStackTrace();
	}

Key:isHighlightInteractedObject

@sameer1sharma
Copy link

usersettings.properties file
isHighlightInteractedObject=false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment