In previous articles I discussed how resource bundles can be used for centralizing the boilerplate text items and how a resource bundle can be implemented in a database table. I also demonstrated I how to support not just different boilerplate text items for different languages but also different sets of boiler plate text for user groups that vary by location, role, department, age or personal preference. One important aspect was not discussed thus far: how to refresh the resource bundle after the application has been started. It is very nice to use a database table to store the resource bundle entries, but one of the suggested advantages is that resource bundle entries can be added and modified while the application is running and these changes would be absorbed without having to redeploy or restart the application. This article will explain how to implement that ‘refresh resource bundle while running’ option.
Steps to add the force refresh resource bundle to an ADF application with database backed resource bundles
This article continues the story that was last told in this article: https://technology.amis.nl/2012/08/13/adding-customization-or-context-sensitivity-to-boilerplate-text-from-database-backed-resource-bundles-in-adf-applications. The application discussed here is the one created in that and previous articles.
The essence of refreshing the resource bundle is also discussed in this article: https://technology.amis.nl/2006/10/14/testing-i18n-features-of-jsf-applications-forcing-a-refresh-of-the-resource-bundles/. The initial findings in this article still hold true today – except that in JDK 6 the option to refresh the cache of resource bundles was built into the JRE as intrinsic option. For Java 6 and later, the code is therefore extremely simple. For previous versions, the code is a little more elaborate.
The implementation consists of two steps. One is the creation of a commandLink that the user can activate to start the refresh. That is easy:
The second half of the implementation consists of the actionListener actionForceBundleRefresh() in the MessageManager class – and the code that this method invokes.
Initially, forceBundleRefresh attempts to use the Java 6 approach: ResourceBundle.clearCache(). That does it at the JVM level – fairly coarse grained!
More fine grained would be to retrieve all individual resource bundles by name and refresh those (and not for example touch resource bundles for other applications running on the same JVM):
When the Java 6 approach does not work, the code calls back to the ‘old fashioned’ alternative. Bundle by bundle is processed and for each bundle, the refreshBundle() method is invoked:
using some helper methods:
To see this in action. Run the application.
Now change one of the resource bundle entries displayed in the page, in the underlying database table:
make the change to this entry that is used for the button label:
Return to the page and press the refresh link:
now the bundle’s cache is emptied and when next a value is requested from the bundle it is reloaded from the database.
Download the ADF application that is discussed in this article: ADFFacesWithResourceBundle_Step5_WithForceRefresh.
Other articles in this series: