Results 1 to 6 of 6

Thread: reload / refresh XmlBeanFactory

  1. #1
    Join Date
    Jan 2005
    Posts
    3

    Default reload / refresh XmlBeanFactory

    hi,

    I'm quiet new to spring so maybe it's a trivial question.

    Is there a way to reload a XmlBeanFactory when the underlying beans.xml has been changed ?[/code]

  2. #2
    Join Date
    Jan 2005
    Posts
    4

    Default

    I am very interested in this too - does Spring have some Observer/Observable pattern going on with the config files?

  3. #3
    Join Date
    Jan 2005
    Posts
    4

    Default

    I found another topic suggesting that one would need to write a custom application context to accomplish this.

    http://forum.springframework.org/showthread.php?t=10432

    This seems odd. My current application can dynamicly reload it's configuration (albeit you have to invoke a servlet that performs the necessary actions), and I need to be able to do the same thing with Spring. One would normally hot deploy the webapp and it would all be reloaded anyway, but unfortunately we are not in the web container.
    All our current configuration is established by a Singleton class that has a reload() method. I suppose we could just convert that class into a Spring Bean and let all the other classes refer to him via DI, but it seems conter-intuitive to using Spring, since the config for all the Spring beans would reside in our own, custom and proprietary type of config file just to get the reloadablity.
    Has anyone worked on / solved this reloadablity issue?
    Last edited by robyn; May 19th, 2006 at 05:15 AM.

  4. #4
    Join Date
    Jan 2005
    Posts
    3

    Default

    I found kind of a solution using FileSystemXmlApplicationContext
    I use a FileWatchDog (org.apache.log4j.helpers.FileWatchdog for example) on my spring beans.xml file and when the file has been changed calll the refresh method on the context.

    This works but I don't know if there's a way to do it directly with Spring (meaning Spring detecting that the file has changed)

  5. #5
    Join Date
    Jan 2005
    Posts
    4

    Default

    That solves the refreshing problem. Nice

    Would you be willing to post some code?

  6. #6
    Join Date
    Jan 2005
    Posts
    3

    Default

    so I have an inner static inner class that inherits from log4j FileWatchDog

    Code:
        private static class DiagnosticFileWatcher extends FileWatchdog
        {
                    
            DiagnosticFileWatcher(String fileName)
            {
                super( fileName);
            }
                  
            protected void doOnChange() 
            {            
                
                reloadConfig();           
            }       
        }

    in the main class I have something like this :

    Code:
      
        public static String SYS_DIAGNOSTIC_CONFIG_FILENAME = "com.something.framework.diagnostics.XMLConfigFile";
        private static String configFileName;
        
        private static DiagnosticFileWatcher fileWatcher =  null;
        
        public static final long FILE_CHECK_INTERVAL = 60 * 1000;   
    
        static 
        {
        	//depending on a system property
        	configFileName = System.getProperty(SYS_DIAGNOSTIC_CONFIG_FILENAME);
        	
        	// initial loadConfig not necessary will be done by the fileWatcher
        	
        	fileWatcher = new DiagnosticFileWatcher(configFileName);
        	fileWatcher.setDelay(FILE_CHECK_INTERVAL);
        	fileWatcher.start();    	
        }
    
    
        public static void reloadConfig()
        {
            if(diagnosticsContext != null)
                diagnosticsContext.refresh();
            else
                initFromFile(configFileName, true);
        }
    
        public static void initFromFile(String fileName, boolean refresh)
        {            
            
        	if(diagnosticsContext == null)
        	{
                String[] configLocations = {configFileName};
                diagnosticsContext = new FileSystemXmlApplicationContext(configLocations, true);
        	}
        	else if(refresh)
        	{
        	    diagnosticsContext.refresh();
        	}
        }
    voila that's not rocket science, I'm not completetly sure it's the best way to do it but it works



    if you have suggestions to improve it let me know

Similar Threads

  1. Proxy Problems Using XmlBeanFactory
    By joesrice in forum AOP
    Replies: 1
    Last Post: Oct 19th, 2005, 03:41 PM
  2. Replies: 1
    Last Post: Aug 4th, 2005, 02:22 AM
  3. Replies: 3
    Last Post: Jun 19th, 2005, 04:53 PM
  4. Refresh PropertyPlaceholder data at runtime
    By rebornspirt in forum Container
    Replies: 2
    Last Post: Oct 28th, 2004, 10:21 AM
  5. XInclude and XmlBeanFactory
    By mattinger in forum Container
    Replies: 1
    Last Post: Sep 27th, 2004, 07:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •