Question

I'm having an issue with my configuration management class, it is not getting reloaded.

Let me show you part of my code:

public class ConfigurationManager extends XMLConfiguration
{
   private static final Logger log = LoggerFactory.getLogger(ConfigurationManager.class);

   private static final long serialVersionUID = 1L;
   public static final String CONFIG_FILE_PATH = "/config.xml";
   private static volatile ConfigurationManager instance = null;
   private static Object lock = new Object();

   // Instance management methods
   public static ConfigurationManager getInstance()
   {
      return getInstance(CONFIG_FILE_PATH);
   }

   public static ConfigurationManager getInstance(String cfg)
   {
      if(instance == null)
      {
         synchronized(lock)
         {
            if(instance == null)
            {
               try
               {
                  instance = new ConfigurationManager(cfg);
                  instance.dumpConfigurationToLog();
               }
               catch(Exception e)
               {
                  log.error("Error calling getInstance. Method params", e);
               }
            }
         }
      }
      return instance;
   }

   private Object loadedCfg;

   private int reloadInterval;

   private void dumpConfigurationToLog()
   {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();

      try
      {
         this.save(bos);
         bos.flush();
      }
      catch(Exception e)
      {
         log.error("Error calling dumpConfigurationToLog. Method params", e);
      }

   }

   @Override
   public void configurationChanged(ConfigurationEvent event)
   {
      log.info("Enter Method configurationChanged params: {}", event);
      if(event.isBeforeUpdate() == false)
      {
         makeUpdates();

         log.info("Configuration file: {} has changed and reloaded...", loadedCfg);
         dumpConfigurationToLog();
      }
      log.info("Return Method configurationChanged");
   }

   private void updateReloadInterval()
   {
      int newReloadInterval = getInt("global.reloadInterval") * 1000;
      if(reloadInterval != newReloadInterval)
      {
         reloadInterval = newReloadInterval;
         if(getReloadInterval() > 0)
         {
            FileChangedReloadingStrategy reloadStrategy = new FileChangedReloadingStrategy();
            reloadStrategy.setRefreshDelay(getReloadInterval());
            this.setReloadingStrategy(reloadStrategy);
         }
         else
            if(getReloadInterval() == 0)
            {
               this.setReloadingStrategy(new InvariantReloadingStrategy());
            }
            else
            {
               log.error("Invalid reload interval for ConfigurationManager: {}", getReloadInterval());
            }
      }
   }

   private ConfigurationManager(String cfgFile) throws Exception, ConfigurationException
   {
      super();
      loadedCfg = cfgFile;
      if(System.class.getResource(cfgFile) != null)
         this.setURL(System.class.getResource(cfgFile));
      else
         this.setURL(getClass().getResource(cfgFile));
      this.load();
      makeUpdates();
      this.addConfigurationListener(this);
      this.setThrowExceptionOnMissing(true);
   }

   private void makeUpdates()
   {
      updateReloadInterval();
   }

   public int getReloadInterval()
   {
      return reloadInterval;
   }
}

Now that code works perfectly fine, I can read the configuration file, and work with it with no major problems, the issue is that it never gets reloaded on configuration changes. I've tried setting breakpoints and so, but it never gets into configurationChanged method.

Does anybody see something wrong here?

Was it helpful?

Solution

Well, after testing and analyzing, I've got to this conclusion, in order to have configurationChanged called, I need to make an explicit call to get values from configuration. And that is something I was not doing.

The thing got fixed when I did that.

OTHER TIPS

You're calling makeUpdates() after setting your ConfigurationListener.

Additionally, calling load() is no guarantee that an Event will get fired.

Lastly, is there anything actually calling addProperty(), etc for this extended class?

Only a small side issue: resource bundles are cached, you can call clearCache, unfortunately not per bundle but per class loader.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top