• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How to load Properties Dynamically at run time?

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have an application which loads the proeprties from file and use them in code, and some times we have to change the properties at regular intervals, in this case every time we have to stop and start the server. This will be a hectic job to rebooting the server in each time. Is there any clue how to read properties at run time even after we change the file?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudhakar,

a naive approach could be to have a background task which reads the properties file at regular intervals and checks for changed properties.

A more efficient solution could be to install an event listener to monitor the properties file for changes and only re-read it in case there was a modification. You could for example use Apache Commons VFS which supports listeners for all kind of virtual file system, i.e. local file systems, too.

Marco
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My preference is for setting up a "status" servlet that can accept a request to reload the properties or other important data files.

Alternately, add the ability to detect a special request to your existing application.

Bill
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Curious, what file-type are you using Sudhakar? XML?
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the original post I'd guess it's a regular properties file!

But it's still unclear if we're talking about a web application or perhaps an own server-like application?!? Bill's idea with a servlet would be a great idea but I'm not sure if "server" is a servlet container or kind of server application... This information would really be helpful to give good advices for a solution

Marco
 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thanks for responding to my query,

we are using properties file and loading properties in Servlets in general way,
Here is the properties looks like


For each user we have to modify the properties, and everytime we are restarting the server (JBOSS).
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like several of the techniques mentioned above will help you out, I'd personally go for the event listener or the service that can request a re-read, they seem pretty elegant solutions.
 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marco, I'll try with the event listener.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd prefer these solutions, too. Unfortunately I don't know how reliable a listener on the file system is in Java. Anyway, even if such a listener would not work as expected Bill's suggestion would be a perfect solution for a web application in any case.

Marco
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm assuming you only have a few applications that access this properties file, otherwise, what I'm about to say below may not make sense.

If you're manually modifying the properties file in a text editor, why not just add a quick web page form on one of your applications (with maybe a quick, hard-coded admin password that only certain people know), and then modify the properties file using that? The Properties file has store() and load() methods and you have it loaded in memory already, so the doing this as a JSP page theoretically wouldn't be too horrible. Then the app itself knows that something changed.

If another application is modifying the properties file, then why not have that application just open a quick HTTP connection to a special URL, like http://host/servlet/app/refreshMe.jsp or whatever, and whenever that application updates the properties file, it hits that URL to tell the other application(s) to reload the properties file. You could also hit this URL in a web browser if you're manually modifying the file. This is the quick and dirty idea. There are more formal ways to do this with web services, with a central web service that then goes and updates all the other apps, etc. etc., but it sounds like you may not need that.

 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works in the following scenario but Im not sure whether its right approach or not.

Earlier My PropertyReader (which reads the properties) class has a method getInstance which is checking for an instance, if its null, then reading property file else just returning the same instance.


Now I commented (see above code) and every time I read the property file, it doesn't matter whether the instance is exists or not and able to returning the new properties.

Here is the way I read the properties at runtime.


I have a question, every time I create the new instance and loading properties, is there any memory problem (load) or any other issue will occur if I follow this approach?
 
reply
    Bookmark Topic Watch Topic
  • New Topic