Showing posts with label Resource Bundle. Show all posts
Showing posts with label Resource Bundle. Show all posts

Monday, December 10, 2007

Resource Bundle Surprise

Not many people knows but Resource Bundle which used to read properties files actually cache the property file content in memory, those changing the content of the properties file at run time wouldn't influence on the application.

The question is how to cause the application to reload the properties file without the need to restart the application server?

The answer is a bit tricky since the resource bundle doesn't expose an API to refresh the cache of the properties file, there is a need to write an MBean which would invoke by the JMX console and this MBean would refresh the properties file content, here is the code for that:

Class aClass = ResourceBundle.getBundle(PROPERTIES_FILE).getClass().getSuperclass();
Field field = aClass.getDeclaredField("cacheList");
field.setAccessible(true);
sun.misc.SoftCache cache = (sun.misc.SoftCache) field.get(null);
cache.clear();


First we get the class of the resource bundle, then we change the cacheList field from private to public (Yes it is possible to change using reflection private memebers to public members) and then get invoke clear method on the cache field.

Links

 
RSS Feeds Submission Directory