问题
We are using JSF2.0 with JDK1.6 and Tomcat6.1
We have a requirement to update the property file values (loaded by JSF resource bundle) without restarting the server so that the live web sessions won't be stopped.
Is it possible with JDK1.6 , i tried the below clearCache code but it didn't work.
ResourceBundle bundle = ResourceBundle.getBundle("Label");
String s = bundle.getString("profile.firstName");
out.println("Value before: %"+ s);
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
bundle = ResourceBundle.getBundle("Label");
s = bundle.getString("profile.firstName");
out.println("Value after: {}"+s);
Has anyone tried the same before.
Update
The below doesn't seems to resolve the problem of reloading the resource bundle
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
ApplicationResourceBundle applicationBundle = ApplicationAssociate.getCurrentInstance().getResourceBundles().get("Label");
Field field = applicationBundle.getClass().getDeclaredField("resources");
field.setAccessible(true);
Map<Locale, ResourceBundle> resources = (Map<Locale, ResourceBundle>) field.get(applicationBundle);
resources.clear();
Am I missing anything?
回答1:
This used to work on some JSF implementations/versions. However, on more recent Mojarra versions the caching mechanism got an extra layer in the implementation itself. Assuming that you're indeed using Mojarra, in addition of the line
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
you also need to do this, starting with com.sun.faces.application.ApplicationAssociate
ApplicationResourceBundle applicationBundle = ApplicationAssociate.getCurrentInstance().getResourceBundles().get("Label");
Field field = applicationBundle.getClass().getDeclaredField("resources");
field.setAccessible(true);
Map<Locale, ResourceBundle> resources = (Map<Locale, ResourceBundle>) field.get(applicationBundle);
resources.clear();
Yes, that's a hack, but as far JSF doesn't provide any clean API methods to achieve the same.
来源:https://stackoverflow.com/questions/5490192/jsf2-0-resourcebundle-needs-to-be-reloaded-without-server-restart