1. Background
When the application starts, it may read the configuration file required by the application. If the content of the configuration file is not static, when the application needs to be updated,
Generally, we upload a new configuration file and restart the server.
If this configuration file is completely controllable for our application, we can load the new configuration file without restarting the server.
2. Implementation steps
(1) The application provides an HTTP interface to receive newly uploaded file data from the client.
(2) The client calls the HTTP interface to upload file data and specifies a class for refreshing data.
(3) The server receives a request, refreshes the file content, and refreshes the memory.
3. Problems Encountered
At the beginning, after receiving the request, the server first writes the data to the file, then reads the file content again, refreshes the memory, and encounters a very strange problem.
When Tomcat server. xml sets the reloadable of the application to true, the file can be overwritten and the memory data can be refreshed, but Tomcat will restart!
When Tomcat server. xml sets the reloadable of the application to false, the file can be overwritten, and the memory refresh method is also executed. However, the memory data is not refreshed.
Never ride elder sister
Possible causes:
(1) When reloadable is true, Tomcat monitors the class of the entire application and restarts if any modification is made. However, we only called a refresh method, and the class file was not updated. Later, we found that as long as the configuration file is refreshed, Tomcat will restart. Therefore,Tomcat is not just a monitoring class, it will monitor all the files under the entire WEB-INF. In addition, reloadable in the online environment should be set to false.
(2) Why is data not refreshed when reloadable is false!
Next, debug the file step by step. Later, I found that although the file is refreshed, the file data read through the refresh method is still old data!
What's wrong with how to read the configuration file?
public static List<String> loadFromClasspath(String filename,String encoding){InputStream in = null;try{in = ConfUtil.class.getClassLoader().getResourceAsStream(filename);return load(in,encoding);}catch(Exception e){throw new RuntimeException(e);}finally{IOUtil.close(in);}}
This is the reason,Tomcat's webappclassloader caches the content of the configuration file. It does not read it from the hard disk every time.Therefore, it is useless to rewrite the configuration file!
4. Solution
There are two solutions:
(1) After obtaining the uploaded data, write the data to the file first, and then directly write the data to the memory instead of reading the file.
(2) When reading a file, you do not need to use webappclassloader to read the file. Instead, you can use fileinputstream to read the file directly from the hard disk.
5. Code Implementation
There are three main classes: refresherfactory, refresher, and xxxrefresher.
Refresherfactory uses the single-instance registry mode to cache xxxrefresher objects.
Refresher is an abstract parent class. You need to override several methods in each subclass to map the file data to the memory.
Xxxrefresher: The specific implementation class.
Source code: http://download.csdn.net/download/goldenfish1919/5173135