1

I have a task to replace a string lookup system which currently relies on mysql. A screen will pass a list of ID's and a language element to a class which then selects the requested strings out of a table in mysql.

I am looking to replace the mysql table with a properties file but I dont want to load all of the properties into the object in one go.

As the strings are used in an old legacy system to render parts of the screens and pdf reports I would want to load them into the properties object as and when they are requested, so first check the properties object and if it doesnt contain my key say "1234Eng" then I would load it into the properties object at that point so its available in future.

The idea being that as there are so many messages I would prefer to only load those which are used on pages people have visited.

Any advice\help is appreciated.

berimbolo
  • 3,319
  • 8
  • 43
  • 78
  • 2
    You can just extend [`ResourceBundle`](http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html) to implement your custom logic - when you call the `static ResourceBundle.getBundle(String bundleName)` it will look for `{bundleName}.class` in the classpath also. Take a look at the [JavaDoc](http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader)) for more details. – Boris the Spider Feb 14 '14 at 11:01
  • If it is possible, use multiple message properties files. – pmverma Feb 14 '14 at 11:05
  • Have a look at [this project](https://github.com/fge/msg-simple): it allows you to do precisely what you want. – fge Feb 14 '14 at 11:05
  • As far as I can tell by looking at the source of PropertyResourceBundle, a resource bundle will load the full properties file at once. – JP Moresmau Feb 14 '14 at 11:08
  • @JPMoresmau the point is that you can implement `MyResourceBundle` that loads properties _however you want_. And then you can do `ResourceBundle.getBundle("MyResourceBundle")` and the API will find it in the classpath. – Boris the Spider Feb 14 '14 at 11:14
  • Boris thanks for this, I am going to have a look at extending ResourceBundle to load on a kind of on the fly basis. I have toyed with creating multiple properties files but the vast majority of them are in the English language with just a few in 2 other languages so... it wouldnt make a great deal of difference. – berimbolo Feb 14 '14 at 11:16
  • 1
    @pao All you would have to do is first sort the properties by key and then do a binary search to find the one you want. I have a feeling that 9000 file IOs will be **much** slower than one big one however... – Boris the Spider Feb 14 '14 at 11:18
  • Ok thanks I will take this into consideration as each screen also does not load a single message but rather an array of ID's with the average being > 10 messages at a time. Perhaps given what you have highlighted and as JP Moresmau suggested this could actually be a waste of time trying to preserve memory at the cost of loads of disk reads. I appreciate the advice, thank you. – berimbolo Feb 14 '14 at 11:20
  • @pao why don't you load the properties files using the `ResourceBundle` API and then use a heap-dump to see how big it all is. I imagine it's smaller than you think... – Boris the Spider Feb 14 '14 at 12:31
  • I will try this out this afternoon, thanks again Boris. – berimbolo Feb 14 '14 at 14:21

1 Answers1

1

Are you really sure having the Properties object in memory is too slow too load and too big to keep around? Check that before going into a more complicated solution. Otherwise, don't reinvent the wheel and use an embedded database. I've had good experiences with SQLite, but there are also some pure Java embedded database, that can give you a mixture of on file and in memory access (see this thread for a comparison).

Community
  • 1
  • 1
JP Moresmau
  • 7,388
  • 17
  • 31
  • I havent actually done any specific bench marking but there are over 9000 strings in the table as I just did a count of them. I did suggest SQLLite to my superiors actually and was told that it is not an option to replace the database, and I need to provide a non database driven solution. As we already use Oracle it would have been easiest to move the tables there... go figure! – berimbolo Feb 14 '14 at 11:13