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 "rootresourcesmessages.txt"
and to create a file object for it like.
File msgFile = new File("rootresourcesmessages.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..
4
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.
1
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.
2