1

我的配置管理类有问题,它没有重新加载。

让我向您展示我的部分代码:

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;
   }
}

现在代码工作得很好,我可以读取配置文件,并且可以毫无问题地使用它,问题是它永远不会在配置更改时重新加载。我试过设置断点等,但它从来没有进入 configurationChanged 方法。

有人看到这里有问题吗?

4

3 回答 3

1

好吧,经过测试和分析,我得出了这个结论,为了调用 configurationChanged,我需要进行显式调用以从 configuration 中获取值。那是我没有做的事情。

当我这样做时,事情就解决了。

于 2012-04-20T14:50:12.403 回答
0

makeUpdates()在设置你的ConfigurationListener.

此外,调用load()并不能保证 Event 会被触发。

最后,这个扩展类有什么实际调用addProperty()等吗?

于 2012-04-18T17:57:29.877 回答
0

只有一个小问题:资源包被缓存,你可以调用clearCache,不幸的是不是每个包而是每个类加载器。

于 2012-04-18T18:08:35.713 回答