中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java實現文件監控器FileMonitor

發布時間:2020-08-09 16:59:29 來源:ITPUB博客 閱讀:180 作者:ckxllf 欄目:編程語言

  應用場景:

  代碼可以實現文件變化后的監聽,如文件變化,自動重新加載文件內容,實現配置文件的熱部署。

  代碼:

  package com.yx.demo.filemonitor;

  import java.io.File;

  import java.lang.ref.WeakReference;

  import java.util.ArrayList;

  import java.util.Collection;

  import java.util.HashMap;

  import java.util.Iterator;

  import java.util.List;

  import java.util.Timer;

  import java.util.TimerTask;

  /**

  * FileMonitor

  * 文件監控器

  *

  * @author yx

  * @date 2019/12/21 0:59

  */

  public class FileMonitor {

  /**

  * 每2秒更新的默認監控器

  */

  private static FileMonitor defaultFileMonitor = new FileMonitor(2 * 1000);

  private Timer timer_;

  private HashMap files_; // File -> Long

  private List fileEntrys = new java.util.concurrent.CopyOnWriteArrayList();

  private Collection> listeners_; // of WeakReference(FileListener)

  private long pollingInterval = 10000;

  public static FileMonitor getDefaultFileMonitor() {

  return defaultFileMonitor;

  }

  /**

  * Create a file monitor instance with specified polling interval.

  *

  * @param pollingInterval Polling interval in milli seconds.

  */

  public FileMonitor(long pollingInterval) {

  this.pollingInterval = pollingInterval;

  files_ = new HashMap();

  listeners_ = new ArrayList>();

  timer_ = new Timer("FileMonitor", true);

  timer_.schedule(new FileMonitorNotifier(), 0, pollingInterval);

  }

  /**

  * Stop the file monitor polling.

  */

  public void stop() {

  timer_.cancel();

  timer_ = null;

  }

  public void start() {

  if (timer_ == null) {

  timer_ = new Timer(true);

  timer_.schedule(new FileMonitorNotifier(), 0, pollingInterval);

  }

  }

  /**

  * Add file to listen for. File may be any java.io.File (including a

  * directory) and may well be a non-existing file in the case where the

  * creating of the file is to be trepped.

  *

  * More than one file can be listened for. When the specified file is

  * created, modified or deleted, listeners are notified.

  *

  * @param file File to listen for.

  */

  public void addFile(String id, File file) {

  if (!files_.containsKey(file)) {

  FileEntry entry = new FileEntry(id, file, file.exists() ? file.lastModified() : -1);

  files_.put(file, entry);

  }

  }

  /**

  * 添加監控文件實體。

  *

  * @param fileEntry

  */

  public void addFileEntry(FileEntry fileEntry) {

  if (!fileEntrys.contains(fileEntry)) {

  fileEntrys.add(fileEntry);

  }

  }

  /**

  * 通過文件實體的標識判斷監控文件實體是否存在。

  *

  * @param id

  * @return

  */

  public boolean fileEntryExists(String id) {

  if (id == null) {

  return false;

  }

  for (int i = 0; i < fileEntrys.size(); i++) {

  if (id.equals(fileEntrys.get(i).getId())) {

  return true;

  }

  }

  return false;

  }

  /**

  * 通過文件實體標識刪除一個監控文件實體。

  *

  * @param id

  */

  public void removeFileEntry(String id) {

  if (id == null) {

  return;

  }

  for (int i = 0; i < fileEntrys.size(); i++) {

  if (id.equals(fileEntrys.get(i).getId())) {

  fileEntrys.remove(i);

  return;

  }

  }

  }

  /**

  * Remove specified file for listening.

  *

  * @param file File to remove.

  */

  public void removeFile(File file) {

  files_.remove(file);

  }

  /**

  * Add listener to this file monitor.

  *

  * @param fileListener Listener to add.

  */

  public void addListener(FileListener fileListener) {

  // Don't add if its already there

  for (Iterator> i = listeners_.iterator(); i.hasNext(); ) {

  WeakReference reference = i.next();

  FileListener listener = (FileListener) reference.get();

  if (listener == fileListener) {

  return;

  }

  }

  // Use WeakReference to avoid memory leak if this becomes the

  // sole reference to the object.

  listeners_.add(new WeakReference(fileListener));

  }

  /**

  * Remove listener from this file monitor.

  *

  * @param fileListener Listener to remove.

  */

  public void removeListener(FileListener fileListener) {

  for (Iterator> i = listeners_.iterator(); i.hasNext(); ) {

  WeakReference reference = (WeakReference) i.next();

  FileListener listener = (FileListener) reference.get();

  if (listener == fileListener) {

  i.remove();

  break;

  }

  }

  }

  /**

  * This is the timer thread which is executed every n milliseconds according

  * to the setting of the file monitor. It investigates the file in question

  * and notify listeners if changed.

  */ 鄭州治療婦科哪個醫院好 http://www.120kdfk.com/

  private class FileMonitorNotifier extends TimerTask {

  @Override

  public void run() {

  try {

  for (Iterator i = fileEntrys.iterator(); i.hasNext(); ) {

  try {

  FileEntry entry = i.next();

  if (entry == null || !entry.check()) {

  i.remove();

  }

  } catch (Throwable t) {

  t.printStackTrace();

  System.out.println("執行文件監控發生錯誤:" + t.getMessage());

  }

  }

  // Loop over the registered files and see which have changed.

  // Use a copy of the list in case listener wants to alter the

  // list within its fileChanged method.

  Collection files = new ArrayList(files_.keySet());

  for (Iterator i = files.iterator(); i.hasNext(); ) {

  File file = i.next();

  try {

  FileEntry fileEntry = files_.get(file);

  long lastModifiedTime = fileEntry.getLastModified();

  long newModifiedTime = file.exists() ? file.lastModified() : -1;

  //logger.debug(file.getAbsolutePath());

  //logger.debug(" {}=>{}", lastModifiedTime, newModifiedTime);

  // Chek if file has changed

  if (newModifiedTime != lastModifiedTime) {

  //logger.debug("file changed {})", file.getAbsolutePath());

  fileEntry.setLastModified(newModifiedTime);

  // Register new modified time

  files_.put(file, fileEntry);

  if (fileEntry.getFileListener() != null) {

  fileEntry.getFileListener().fileChanged(fileEntry);

  } else {

  // Notify listeners

  for (Iterator> j =

  listeners_.iterator(); j.hasNext(); ) {

  WeakReference reference =

  (WeakReference) j.next();

  FileListener listener = (FileListener) reference.get();

  // Remove from list if the back-end object has been GC'd

  if (listener == null) {

  j.remove();

  } else {

  listener.fileChanged(fileEntry);

  }

  }

  }

  }

  } catch (Throwable t) {

  if (file != null) {

  t.printStackTrace();

  System.out.println(

  "file monitor execute error, file=" + file.getAbsolutePath() +

  t.getMessage());

  } else {

  System.out.println(

  "file monitor execute error, file=null" + t.getMessage());

  }

  }

  }

  } catch (Throwable t) {

  System.out.println("執行文件監控發生錯誤" + t.getMessage());

  }

  }

  }

  }

  package com.yx.demo.filemonitor;

  /**

  * FileListener

  *

  * @author yx

  * @date 2019/12/21 0:55

  */

  public interface FileListener {

  /**

  *

  * @param fileEntry

  */

  public void fileChanged(FileEntry fileEntry);

  }

  package com.yx.demo.filemonitor;

  import java.io.File;

  import java.lang.ref.WeakReference;

  /**

  * FileEntry

  * 文件Entry,如果FileEntry指定了FileListener,那么當文件發生變動時只觸發指定的FileListener

  *

  * @author yx

  * @date 2019/12/21 0:56

  */

  public class FileEntry {

  String id;

  File file;

  long lastModified;

  FileListener fileListener = null;

  Object userData;

  WeakReference

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

苍溪县| 策勒县| 廉江市| 云龙县| 河南省| 伊金霍洛旗| 桐梓县| 五常市| 安化县| 搜索| 监利县| 游戏| 正蓝旗| 乐陵市| 梁山县| 云南省| 毕节市| 美姑县| 伊通| 石泉县| 宣武区| 察哈| 渭源县| 绿春县| 叙永县| 揭阳市| 广灵县| 河西区| 嘉义县| 乐业县| 洪洞县| 秦安县| 常德市| 塘沽区| 胶南市| 和静县| 新干县| 浦江县| 金川县| 右玉县| 古田县|