1

What are the different ways to resolve UI messages and provide internalization support in an web application using spring framework?

We use property files and ResourceBundleMessageSource to resolve messages in Spring. Spring's implementation causes an high cpu usage in our application. There are two problems with ResourceBundleMessageSource implementation.

  1. Locking contention - getResourceBundle() and getMessageFormat() are both synchronized.
  2. MissingResourceException - Resolving a message involves looping through all the resource bundles defined in the application and calling bundle.getString(key). bundle.getString(key) method call throws an MissingResourceException if the key is not found. Searching for the key until we find the message for the given key. Since exception construction is a slow process and could eat up the CPU (which is what I observed in our load tests) this looks like a bottleneck.

Though there are workarounds for both of the above problems (by extending the class and overridding the behavior) I wanted to know if there are other ways in spring framework to provide internationalization support to a web application.

Thanks in advance

Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113

1 Answers1

2

You can use the ReloadableResourceBundleMessageSource instead. It provides some internal caching.

If that does not work, I would advise implementing your own MessageSource (It's fairly straight forward). Spring provides AbstractMessageSource which may be helpful to start with.

From there, you can implement some caching. More than likely, your localizations are not being updated frequently.

You can read here on using the new Cacheable annotations in Spring 3.1

spring 3.1 @Cacheable example

I have done this already with success in applications that allow admins to override locales in the database. Your particular implementation however would obviously be very different.

Community
  • 1
  • 1
aweigold
  • 6,532
  • 2
  • 32
  • 46
  • You are right. Implementing a custom MessageSource would be a solution to this. The custom implementation could resolve messages for preconfigured locales on start up. ReloadableResourceBundleMessageSource would not help since it doesn't target to resolve the two issues I mention above. – Andy Dufresne Oct 10 '12 at 04:58
  • I implemented two MessageSource implementations. One which resolves the above two issues through workarounds and another implementation which resolves the messages and caches them for future references (The resource bundle and message format instances are not cached). Here is how the first implementation looks - http://amitstechblog.wordpress.com/2012/09/17/issues-with-springs-resourcebundlemessagesource/ – Andy Dufresne Oct 10 '12 at 05:01