有没有办法知道,在ServletContextListener中,Web应用程序中哪个文件被更改导致上下文重新加载? 它是一个JSP文件吗?我可以找出哪个.class文件已被更改?
答案 0 :(得分:0)
您可以实现文件更改侦听器
public class FileMonitor
{
private static Logger logger = Logger.getLogger(FileMonitor.class);
private static final FileMonitor instance = new FileMonitor();
private Timer timer;
private Map timerEntries;
private FileMonitor() {
this.timerEntries = new HashMap();
this.timer = new Timer(true);
}
public static FileMonitor getInstance() {
return instance;
}
/**
* Add a file to the monitor
*
* @param listener The file listener
* @param filename The filename to watch
* @param period The watch interval.
*/
public void addFileChangeListener(FileChangeListener listener,
String filename, long period) {
this.removeFileChangeListener(filename);
logger.info("Watching " + filename);
FileMonitorTask task = new FileMonitorTask(listener, filename);
this.timerEntries.put(filename, task);
this.timer.schedule(task, period, period);
}
/**
* Stop watching a file
*
* @param listener The file listener
* @param filename The filename to keep watch
*/
public void removeFileChangeListener(String filename) {
FileMonitorTask task = (FileMonitorTask)this.timerEntries.remove(filename);
if (task != null) {
task.cancel();
}
}
private static class FileMonitorTask extends TimerTask {
private FileChangeListener listener;
private String filename;
private File monitoredFile;
private long lastModified;
public FileMonitorTask(FileChangeListener listener, String filename) {
this.listener = listener;
this.filename = filename;
this.monitoredFile = new File(filename);
if (!this.monitoredFile.exists()) {
return;
}
this.lastModified = this.monitoredFile.lastModified();
}
public void run() {
long latestChange = this.monitoredFile.lastModified();
if (this.lastModified != latestChange) {
this.lastModified = latestChange;
this.listener.fileChanged(this.filename);
}
}
}
}
代码不是我的,我找不到作者atm我会在发现后立即更新。