2

I would like to have the ability to periodically check some properties file and apply changes in my app accordingly.

In my java-spring application I'm checking periodically whether the properties file loaded from the file-system has changed:

Resource propsResource = new FileSystemResource("/path/to/properties/file");
File propertiesFile = propsResource.getFile();
long currentTimeStamp = propertiesFile.lastModified();
if (currentTimeStamp != lastModified) {
//File Changed
}

In case it has changed I would like to reload my spring beans since some of them are actually loaded conditionally relaying on the properties values in the properties file.

Using the refresh method did not do the trick so I'm actually calling the close method and recreating my context:

context.close();
context = new AnnotationConfigApplicationContext(AppConfig.class);

This does the trick. My problem is that I have some beans that contain cached data and I don't want to lose this data. One solution is to keep using this method but make sure my cached data resides in classes that are not managed by spring (either static or singletones managed by me).

Another solution could be to copy my cached data before recreating the context and then set it back to the newly create beans (those that hold the cached data) but this feels super ugly.

Is there a better approach for this?

forhas
  • 11,551
  • 21
  • 77
  • 111
  • the problem is that injected bean takes the scope of a parent bean. f.ex. you inject prototype to singleton. thus this leads to that changing the configuration do not take affect. I think you should take a closer look to spring aop proxing. what you can do with it is that you can inject proxy bean and replace the original object inside proxy when configuration is changed. – hahn Jul 29 '15 at 13:21
  • Most of the beans should be reloaded, there are only a few that should not – forhas Jul 30 '15 at 11:21
  • 1
    it's difficult to predict all nuances but in general, you could then split 'static' beans that are not supposed to be reloaded and 'dynamic' by using additional applicationContext. you can use AbstractApplicationContext#setParent method for creating a hierarchy. in the top (parent) context should reside beans with cache and lower (child) context would be restarted/reloaded or what so ever – hahn Jul 30 '15 at 12:01
  • This sounds interesting (and creative!). I sure know the concept of one context (the parent) imports another (the child) but I'm not familiar with the other direction meaning setting using "setParent" method to set the child's parent. I will give it a shot! – forhas Jul 31 '15 at 16:53
  • @Babur I have a problem implementing your solution since I cannot create the "child" context, it has dependencies to the "parent" since the application beans relays upon the beans that hold my cached data. Once I call the "new" keyword for the child context it crashes, naturally. – forhas Aug 02 '15 at 10:50
  • ok, could it be an option to use org.springframework.aop.target.HotSwappableTargetSource with combination org.springframework.aop.framework.ProxyFactoryBean? – hahn Sep 01 '15 at 10:12

1 Answers1

-1

i would rather not just reloading the Context files on your own, let the jvm take care of it.

For the problem which you are facing, you might want to check out the Watcher service provided within Jdk 1.7 and above. What it does is, it checks if the files in a particular directory are modified, if it is modified you can override the method to do the things you wanted to do.

Watcher service in jdk 1.7

chetank
  • 392
  • 3
  • 17
  • Maybe I misunderstood but how does your answer resolves my problem? – forhas Jul 30 '15 at 11:18
  • if i understand correctly, its just a properties file, when u put a watcher service, any file that is been changed is been notified to the service and i believe thats what you want. – chetank Jul 30 '15 at 14:02
  • Did you even read the question?? Did you read the title?? I quote myself: "My problem is that I have some beans that contain cached data and I don't want to lose this data". How does your answer helps resolving my problem? – forhas Jul 30 '15 at 14:11
  • and you think, reloading the application context saves the cached data????. Anyways man i think helping is a crime here – chetank Jul 30 '15 at 14:41
  • No I don't. You still don't get me, but we are not talking about me, we are talking about your answer - can you tell how does it resolve my problem? Next time please don't waste people's time with irrelevant answers. Have a nice day – forhas Jul 30 '15 at 14:45