一种比较简陋的方法:
1 ActionListener taskPerformer = new ActionListener() {
2 public void actionPerformed(ActionEvent evt) {
3 log.info("monitor is running at " + new java.util.Date());
4 String configfile = (String)getServletContext().getAttribute("configfile");
5 if(configfile != null && configfile.length()!=0){
6 try{
7 File file = new File(configfile);
8 if(file.lastModified() > lastModifyTime){
9 lastModifyTime = file.lastModified();
10 loadProp();
11 }
12 }catch(Exception e){
13 log.error("construct file:" + configfile + " exception");
14 e.printStackTrace();
15 }
16 }
17 }
18 };
19
20 //启动监听线程
21 new Timer(delay, taskPerformer).start();
来自geosoft.no的解决方法:
1 import java.io.File;
2
3 /**
4 * Interface for listening to disk file changes.
5 * @see FileMonitor
6 *
7 * @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
8 */
9 public interface FileListener
10 {
11 /**
12 * Called when one of the monitored files are created, deleted
13 * or modified.
14 *
15 * @param file File which has been changed.
16 */
17 void fileChanged (File file);
18 }
1 import java.util.*;
2 import java.io.File;
3 import java.lang.ref.WeakReference;
4
5 /**
6 * Class for monitoring changes in disk files.
7 * Usage:
8 *
9 * 1. Implement the FileListener interface.
10 * 2. Create a FileMonitor instance.
11 * 3. Add the file(s)/directory(ies) to listen for.
12 *
13 * fileChanged() will be called when a monitored file is created,
14 * deleted or its modified time changes.
15 *
16 * @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
17 */
18 public class FileMonitor
19 {
20 private Timer timer_;
21 private HashMap files_; // File -> Long
22 private Collection listeners_; // of WeakReference(FileListener)
23
24
25 /**
26 * Create a file monitor instance with specified polling interval.
27 *
28 * @param pollingInterval Polling interval in milli seconds.
29 */
30 public FileMonitor (long pollingInterval)
31 {
32 files_ = new HashMap();
33 listeners_ = new ArrayList();
34
35 timer_ = new Timer (true);
36 timer_.schedule (new FileMonitorNotifier(), 0, pollingInterval);
37 }
38
39
40
41 /**
42 * Stop the file monitor polling.
43 */
44 public void stop()
45 {
46 timer_.cancel();
47 }
48
49
50 /**
51 * Add file to listen for. File may be any java.io.File (including a
52 * directory) and may well be a non-existing file in the case where the
53 * creating of the file is to be trepped.
54 * <p>
55 * More than one file can be listened for. When the specified file is
56 * created, modified or deleted, listeners are notified.
57 *
58 * @param file File to listen for.
59 */
60 public void addFile (File file)
61 {
62 if (!files_.containsKey (file)) {
63 long modifiedTime = file.exists() ? file.lastModified() : -1;
64 files_.put (file, new Long (modifiedTime));
65 }
66 }
67
68
69
70 /**
71 * Remove specified file for listening.
72 *
73 * @param file File to remove.
74 */
75 public void removeFile (File file)
76 {
77 files_.remove (file);
78 }
79
80
81
82 /**
83 * Add listener to this file monitor.
84 *
85 * @param fileListener Listener to add.
86 */
87 public void addListener (FileListener fileListener)
88 {
89 // Don't add if its already there
90 for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
91 WeakReference reference = (WeakReference) i.next();
92 FileListener listener = (FileListener) reference.get();
93 if (listener == fileListener)
94 return;
95 }
96
97 // Use WeakReference to avoid memory leak if this becomes the
98 // sole reference to the object.
99 listeners_.add (new WeakReference (fileListener));
100 }
101
102
103
104 /**
105 * Remove listener from this file monitor.
106 *
107 * @param fileListener Listener to remove.
108 */
109 public void removeListener (FileListener fileListener)
110 {
111 for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
112 WeakReference reference = (WeakReference) i.next();
113 FileListener listener = (FileListener) reference.get();
114 if (listener == fileListener) {
115 i.remove();
116 break;
117 }
118 }
119 }
120
121
122
123 /**
124 * This is the timer thread which is executed every n milliseconds
125 * according to the setting of the file monitor. It investigates the
126 * file in question and notify listeners if changed.
127 */
128 private class FileMonitorNotifier extends TimerTask
129 {
130 public void run()
131 {
132 // Loop over the registered files and see which have changed.
133 // Use a copy of the list in case listener wants to alter the
134 // list within its fileChanged method.
135 Collection files = new ArrayList (files_.keySet());
136
137 for (Iterator i = files.iterator(); i.hasNext(); ) {
138 File file = (File) i.next();
139 long lastModifiedTime = ((Long) files_.get (file)).longValue();
140 long newModifiedTime = file.exists() ? file.lastModified() : -1;
141
142 // Chek if file has changed
143 if (newModifiedTime != lastModifiedTime) {
144
145 // Register new modified time
146 files_.put (file, new Long (newModifiedTime));
147
148 // Notify listeners
149 for (Iterator j = listeners_.iterator(); j.hasNext(); ) {
150 WeakReference reference = (WeakReference) j.next();
151 FileListener listener = (FileListener) reference.get();
152
153 // Remove from list if the back-end object has been GC'd
154 if (listener == null)
155 j.remove();
156 else
157 listener.fileChanged (file);
158 }
159 }
160 }
161 }
162 }
163
164
165 /**
166 * Test this class.
167 *
168 * @param args Not used.
169 */
170 public static void main (String args[])
171 {
172 // Create the monitor
173 FileMonitor monitor = new FileMonitor (1000);
174
175 // Add some files to listen for
176 monitor.addFile (new File ("D:\\myjava\\JCreatorWorkspace\\FileMonitor\\test.txt"));
177
178 // Add a dummy listener
179 monitor.addListener (monitor.new TestListener());
180
181 // Avoid program exit
182 while (!false) ;
183 }
184
185
186 private class TestListener
187 implements FileListener
188 {
189 public void fileChanged (File file)
190 {
191 System.out.println ("File [" + file.getName() + "] changed At:" + new java.util.Date());
192 }
193 }
194 }
195
196
197