views:

81

answers:

2

Hello

I have an application which uses a servlet to read an intialization parameter from web.xml for the location of a property file. The serlvet then creates an instance of a class which stores the location of the file for other programs to read as required. This class has appropriate get and set methods.

But my question concerns access to the properties: should the physical property file be read by each program as at runtime or should the properties be stored in memory instead?

The properties are not currently altered at runtime but this could change? I've seen various alternative approaches but am not sure which is best.

Thanks

Mr Morgan

+4  A: 

If the properties file is not large, then storing it in memory may be more efficient than reading it from disk everytime. You may want to consider Apache Commons Configuration. It supports automatic reloading.

BalusC
This seems the best approach. The file is not large - perhaps 50 - 100 entries in all. Thanks.
Mr Morgan
+1 i wasnt aware of this apache commons class.
daedlus
@Mr Morgan: you may also consider to put propertiesfile in classpath or to add its path to the classpath, so that you can just use the filename to load it.
BalusC
@BalusC can you tell me if it is difficult to add the automatic loading to code: fis = new FileInputStream(PropertyFiles.myApp_PropertiesFile()); Properties p = new Properties(); p.load(fis); value = p.getProperty(key); Where this code is set in an intialization servlet? PropertyFiles.myApp_PropertiesFile simply stores the location of the properties file.
Mr Morgan
You'll need to write code which runs in a background thread, checks the last modified timestamp of the file at certain intervals and reloads the properties if necessary. Apache Commons Configuration has already done this all for you on a robust manner.
BalusC
Thanks. i'll look at this later.
Mr Morgan
+1  A: 

Do you want to server values from .properties file? First load the properties from the file and cache them.And server it from the cache when required. Have a file watch dog that monitors the file say every x sec and reload the properties from file when the file is modified.

daedlus