Description
Igor Stepanov opened SPR-14583 and commented
Hello Spring team,
I'd like to use ReloadableResourceBundleMessageSource to implement REST service to access localized content. And to achieve best performance, I'd like to support caching with "if-modified-since". So the idea is to rely on PropertiesHolder.getFileTimestamp() method to understand if some of the underlying property files was updated or not.
I've extended ReloadableResourceBundleMessageSource and call next method:
protected PropertiesHolder getMergedProperties(Locale locale) {
PropertiesHolder mergedHolder = this.cachedMergedProperties.get(locale);
if (mergedHolder != null) {
return mergedHolder;
}
Properties mergedProps = newProperties();
mergedHolder = new PropertiesHolder(mergedProps, -1);
String[] basenames = StringUtils.toStringArray(getBasenameSet());
for (int i = basenames.length - 1; i >= 0; i--) {
List<String> filenames = calculateAllFilenames(basenames[i], locale);
for (int j = filenames.size() - 1; j >= 0; j--) {
String filename = filenames.get(j);
PropertiesHolder propHolder = getProperties(filename);
if (propHolder.getProperties() != null) {
mergedProps.putAll(propHolder.getProperties());
}
}
}
PropertiesHolder existing = this.cachedMergedProperties.putIfAbsent(locale, mergedHolder);
if (existing != null) {
mergedHolder = existing;
}
return mergedHolder;
}
However in current implementation property fileTImestamp is initialized with -1 and never gets updated, so it's not really useful. What do you think about updating it with latest fileTimestamp of the PropertiesHolder instances which are merged into this one?
Moreover, what about adding some setPropertiesChangedCallback() method to make it possible to subscribe to the file updates? In my case this change will allow to push changes to consumer through some message bus and prevent repeated checking through REST interface.
Affects: 4.3.2
Issue Links:
- Race condition in ReloadableResourceBundleMessageSource since 4.1.0 [SPR-12177] #16791 Race condition in ReloadableResourceBundleMessageSource since 4.1.0
- ReloadableResourceBundleMessageSource locks properties hashmap and fails under load. [SPR-10500] #15133 ReloadableResourceBundleMessageSource locks properties hashmap and fails under load.
- ResourceBundleMessageSource should allow for custom PropertyResourceBundle subclass [SPR-12666] #17265 ResourceBundleMessageSource should allow for custom PropertyResourceBundle subclass
- Allow adding resources to ReloadableResourceBundleMessageSource [SPR-10314] #14948 Allow adding resources to ReloadableResourceBundleMessageSource