| Author |
ResourceBundle design problem
|
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
Hello all i have web application that using ResourceBundle as its system for multy language every caption in the system will be taken from the properties file the web application is running on tomcat server .now here is how it works . i have main properties file that called "global.properties" this file hold the path to other properties files the structure is like that default = /en/utf8/default objtype = /objtype menuitems = /en/utf8/menuitems ... ... and so on now when there is to transform caption from the GUI to its string the function first will check every key and every properties file its point to if the key from the GUI exist if its not it will go to the next key and and to the next properties file it points to , this method slows down the GUI loading very much in the global.properties i have like 40 keys holding pointers to 40 properties file s and in single jsp page there can be like 40 captions that needs to be translate to strings . how can i make it work better and faster im sure im doing it in the wrong way .
|
 |
Amit M Tank
Ranch Hand
Joined: Mar 28, 2004
Posts: 257
|
|
|
Could you please paste some snippet of your code here? Are you using PropertiesResourseBundle? If you are using PropertiesResourceBundle it will automatically cache the data and there should not be such performance issues.
|
Amit Tank
Linked In
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
yeah sure here you go
|
 |
Amit M Tank
Ranch Hand
Joined: Mar 28, 2004
Posts: 257
|
|
The reason your code is slow is because you are reading the properties file again for each key lookup. Here I would advice you to have a Singleton Class to be used for this purpose which has a HashMap containing all the ResourceBundle you are using in the code. Also Instead of using the FileInputStream to read the properties file use the ClassLoader to get the inputstream. Or alternativley use ResourceBundle.getBundle() method. Implementations of getBundle may cache instantiated resource bundles and return the same resource bundle instance multiple times. They may also vary the sequence in which resource bundles are instantiated as long as the selection of the result resource bundle and its parent chain are compatible with the description above See Documentation here Let me know if it works [ July 05, 2007: Message edited by: Amit M Tank ]
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
|
thanks for the fast reply befoor i do that i like to know what is the difference between this.getClass().getClassLoader().getResourceAsStream and FileInputStream
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
on more thing i cant use this.getClass().getClassLoader().getResourceAsStream becose the method is static and i can't use this. in it how can i overcome this?
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
ok solved that , one thing i didnt said i like to avoid to add the files locations to the claspath this is why im using full paths from the first place , this is to avoid more configuration when installing the application (... dont ask i just cant add to class path ...)
|
 |
Amit M Tank
Ranch Hand
Joined: Mar 28, 2004
Posts: 257
|
|
That will insure that the file is loaded by the proper class loader. You can also use You could also use InputStream. But the main Idea is to write a code such that it doesn't read the files again and again, so caching the ResourceBundle Object is recommended
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
|
but doesn't the ResourceBundle cache it automateclly ?
|
 |
Amit M Tank
Ranch Hand
Joined: Mar 28, 2004
Posts: 257
|
|
|
Yes it does, if you use ResourceBundle.getBundle() method, It is a factory which is may return you the same cached instance again and again (as per java docs).
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
ok i understand , i have another question , if i build this single tone bundles cache what will be better and faster way ? to do hash table and save as many elements as the number of the properties files or to build one Properties object from all the properties files so when it look for akey it will patch it from the Properties object and not go throw finding it first in the hash table and then in the Properties object that is associated to that key ? what is better for performance ? another question is , is there way to force the ResourceBundle mechanism to use its cache even so i don't use getbundle()?
|
 |
Amit M Tank
Ranch Hand
Joined: Mar 28, 2004
Posts: 257
|
|
If you use a single hashmap to store a key-value pair for all your properties file then the key will be overwritten by the last entry of the property which is encountered. Is your implementation/business requirement okay with that? Of course it will be faster then the other first approach. You need to have a look at the ResourceBundle source code which ships with the JDK so as to get the answer of the second question. But I have used ResourceBundle.getBundle with JDK 1.4_02 and I have seen the my properties files were getting cached even though I didn't wanted them to be cached.
|
 |
 |
|
|
subject: ResourceBundle design problem
|
|
|