package com.jiayue.ipfcst.fileupload.util;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;

/**
 * 上报文件目录监控
 *
 * @author xsl
 * @version 3.0
 * @since 2020/4/13 19:00
 */
public class FileUploadMonitor {
  FileAlterationMonitor monitor = null;

  public FileUploadMonitor(long interval) throws Exception {
    monitor = new FileAlterationMonitor(interval);
  }

  //监控方法
  public void monitor(String path, FileAlterationListener listener) {
    FileAlterationObserver observer = new FileAlterationObserver(new File(path));
    monitor.addObserver(observer);
    observer.addListener(listener);
  }

  //监控停止
  public void stop() throws Exception {
    monitor.stop();
  }

  //监控开始
  public void start() throws Exception {
    monitor.start();
  }
}
