Question: In java application, How to change error message or simple alert text without redeploying / restarting application

Closest answers I could think up (1) is to have a new error messages in specific file in specific location say "\root\resources\messages.txt"and to create a file object for it like.

File msgFile = new File("\root\resources\messages.txt");

msgFile.exist() will return true or false based on the result we can display pre-define message defined else where in application or use this message

(2) having a property file can sound the same but it'll be loaded only once when application is started so we have to restart

Is there anyother way to achieve this? what better place to ask than here..

  • 1
    I think in order to get a proper answer you'd need to tell why do you want to do this. Is it for easier testing and development? – Memleak Sep 12 '14 at 12:02
  • for better maintenance, if I can change message without restarting it'll be awesome. Says its a web application logged on users won't lose session. – Shiva Sep 12 '14 at 12:51
  • You might consider dynamic runtime class loading but that might be quite difficult to implement and debug. – Basile Starynkevitch Sep 12 '14 at 13:37
  • Thanks for the input Basile but Reflection is quite costly and as you pointed out difficult to implement (and maintain) – Shiva Sep 12 '14 at 14:08

The most common way to handle updating messages without a redeploy/restart is via database or similar system like the file you proposed. Another option is to add functionality to the app itself to change messages, this would make use of the properties file.

Using a database is probably the best approach, especially if you already have one set up for your app the maintenance and scalability are just better here. You could use a file early on especially if you only have a few messages to store, there would be an IO hit, but it shouldn't be noticeably different than calls out to other methods of getting a dynamic message. The big problem with using a file is that the IO cost will increase as the file size increases if you store messages in a sequential manner, If you use multiple files managing the sheer number of files becomes difficult. Its possible to create a file with a fixed with for each message and keep track of the position of each message in the file to cut down IO costs of a large file (ie make a binary file), but at this point a database would be less work.

| improve this answer | |
  • Totally agreed. Say I have 1000 error codes and corresponding messages, having it in DB totally makes sense. One DB call with primary key as error code. – Shiva Sep 12 '14 at 14:10

Obviously the message needs to be defined outside the application, so the simplest solution is to put it in a file. But you don't want to read a file every single time you want to display the alert; that's a lot of unnecessary IO. You can use Java's Watch Service API to check if the file containing the message has changed, otherwise you can continue displaying the same message from the last time you read the file.

You can find a tutorial here.

| improve this answer | |
  • Do you mean to say we can have such watch service for prop file and when it changes we can update the properties? if so its cool, I knew we can monitor but was not able to connect both thanks – Shiva Sep 12 '14 at 14:03
  • @Shiva Yes. It doesn't matter what kind of file you use, the important thing is that you can re-read it whenever it changes and update whatever variable holds the message string in your application. – Doval Sep 12 '14 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.