JNotify,一個支持動態監控文件和文件夾(支持級聯監控)的架包。在linux系統中,調用的是linux底層的inotify服務,只是添加了對子文件夾級聯監控的功能。在windows中,需要添加附件的dll文件,因為windows默認沒有該服務
一、郵件系統對本地文件夾的監聽
1、使用 jnotify 需要導入 jnotify.jar
2、需要在jdk/bin 添加jnotify 本地方法:jnotify.dll jnotify_64bit.dll
二、步驟
1、jnotify監聽目錄
public class MailWatcher {
/** 日志 */
private static final Logger LOG = LoggerFactory
.getLogger(MailWatcher.class);
// 收件路徑
private String path;
public MailWatcher() {
// 收件郵件路徑
path = Configure.getString("path");
}
// 監聽主線程
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
try {
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
// 監聽接收郵件
int watchId = JNotify.addWatch(path, mask, true, new MailListener());
// 是否監聽子目錄
boolean watchSubtree = true;
// 日志
LOG.info("watcher開始監聽:{} {}", path, watchId);
} catch (Exception e) {
LOG.error("監聽失敗", e);
}
}
}).start();
// 不讓主線程掛掉
while (true) {
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2、監聽器,
//文件創建 動態監聽
public class MailListener implements JNotifyListener {
/** 日志 */
private static final Logger LOG = LoggerFactory.getLogger(MailListener.class);
//文件重命名時
public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
LOG.debug("{} renamed to {}", oldName, newName);
}
//文件修改時
public void fileModified(int wd, String rootPath, String name) {
LOG.debug("{} modified", name);
}
//文件刪除時
public void fileDeleted(int wd, String rootPath, String name) {
LOG.debug("{} deleted", name);
}
//文件創建時
public void fileCreated(final int wd, final String rootPath, final String name) {
if (name.endsWith(".eml")) {
LOG.info("{} created", name);
new Thread(new Runnable() {
@Override
public void run() {
LOG.info("{},開始讀取", name);
MimeMessage msg = null;
File file = new File(rootPath, name);
try {
Thread.sleep(Configure.getLong("wait_first_time"));
} catch (Exception e) {
LOG.warn("{},初始等待失敗", name);
}
// 再循環等待50次,每次等待10秒,共計500秒
// 寫入郵件需要時間,所以循環
for (int i = 0; i < Configure.getInt("wait_times"); i++) {
try {
//這裡寫的封裝,file--->mimemessage
msg = EmlFileHelper.getMimeFile(file);
break;
} catch (Exception e) {
LOG.debug("第{}次讀取文件{}失敗", i + 1, name, e);
}
try {
Thread.sleep(Configure.getLong("wait_time_interval") * 1000l);
} catch (InterruptedException e) {
LOG.debug("等待失敗", e);
}
}
if (msg == null) {
LOG.error("{},重試後讀取失敗", name);
return;
}
if (!Configure.getBoolean("not_intercept_send_email")) {
// 判斷是否是發出的郵件,如果是,就不記錄
try {
String receiver = name.substring(0, name.indexOf(File.separator));
//這裡是我寫的郵件幫助類,忽略
String from = MailHelper.getFrom(msg);
if (from.startsWith(receiver + "@")) {
LOG.info("{}是發出的郵件,不記錄", name);
return;
}
} catch (Exception e) {
LOG.error("判斷是否是發出的郵件失敗", e);
}
}
try {
saveMsg(msg, name);
LOG.info("{},保存成功", name);
} catch (Exception e) {
LOG.error("{},保存失敗", name, e);
}
}
}).start();
}
}
}
3、開啟監聽,
public class Main {
public static void main(String[] args) throws Exception {
new MailWatcher().start();
}
}
4、說明,在main中開啟主線程,文件修改觸發jnotify 監聽方法,