JSF2.0 ResourceBundle needs to be reloaded without server restart

China☆狼群 提交于 2020-01-03 02:29:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!