1

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?

Fernando Moyano
  • 1,097
  • 4
  • 15
  • 29
  • *"Does anybody see something wrong here?"* Yes, noise like this.. *"Thanks in advance, Fer"* Please leave it out in future. – Andrew Thompson Apr 18 '12 at 17:48
  • *"it never gets into configurationChanges method."* What `configurationChanges` method? For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Apr 18 '12 at 17:49
  • @AndrewThompson, the configurationChanges method is inside the code shown. The whole code is for the ConfigurationManager class, and inside it the configurationChanged method is declared (it is an override). Isn't it visible? – Fernando Moyano Apr 18 '12 at 18:00
  • Thanks again @AndrewThompson, sorry about that, you're right, after replying to you I've realized that the text was not consistent with the code and then I've fixed it. In the other hand, thanks for the SSCCE link, I'll try to follow it now and fix the code up based on it. – Fernando Moyano Apr 18 '12 at 19:46

3 Answers3

1

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.

Fernando Moyano
  • 1,097
  • 4
  • 15
  • 29
0

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?

Spencer Kormos
  • 8,381
  • 3
  • 28
  • 45
0

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

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138