Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ResourceBundle Performance

Tags:

I am using a ResourceBundle and Locale to lookup property values. Quite simply, the code looks like this:

  public static String getPropertyValue(Locale locale, String resourceName, String key) {     ResourceBundle resource = ResourceBundle.getBundle(resourceName, locale);     return resource.getString(key);   } 

My question is about performance. Would a caching approach be quicker or a better implementation than accessing property files on the classpath? My understanding is that ResourceBundle performance is very good in general.

The properties file (in this case) is fewer than 30 lines (i.e., ~30 key/value pairs).

I question the performance since we could use a similar approach on high-load pages, and the lookup-on-demand approach might prove costly.

like image 250
Chocula Avatar asked Dec 01 '09 23:12

Chocula


People also ask

What is the ResourceBundle class in Java?

ResourceBundle class is used to store text and objects which are locale sensitive. Generally we use property files to store locale specific text and then represent them using ResourceBundle object.

What is ResourceBundle in spring?

Spring's application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle. MessageSource is an interface that defines several methods for resolving messages.

What is ResourceBundle getBundle?

getBundle(String baseName) method gets a resource bundle using the specified base name, the default locale, and the caller's class loader.


1 Answers

According to the Javadocs:

Resource bundle instances created by the getBundle factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached.

So you shouldn't need to do caching on your own. But if you need finer-grained control of the caching behavior, you can use the getBundle(String, ResourceBundle.Control) overload and pass a customized Control in.

like image 124
Michael Myers Avatar answered Sep 17 '22 13:09

Michael Myers