应对不同事件我们只需要在对应平台下添加对应事件监听即可。
此为监听文件创建、改变、删除、读、写等操作
public void fileRenamed(int wd, String rootPath, String oldName, String newName) { System.out.println("[" + dfFilesNotify.format(new Date()) + "]"); System.out.println("watch-notify renamed " + rootPath + " : " + oldName + " ---> " + newName); if ((rootPath == null) || (newName == null) || newName.endsWith("swp") || newName.endsWith("swx") || newName.endsWith("~") || newName.endsWith("swpx")) { return; } try { notifyKafka(rootPath, newName, "FILE_RENAMED"); } catch (Exception e) { System.out.println("call FileParseService failed, " + e); } } public void fileModified(int wd, String rootPath, String name) { System.out.println("[" + dfFilesNotify.format(new Date()) + "]"); System.out.println("watch-notify modified " + rootPath + " : " + name); try { notifyKafka(rootPath, name, "FILE_NOWRITTEN_CLOSED"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void fileDeleted(int wd, String rootPath, String name) { System.out.println("[" + dfFilesNotify.format(new Date()) + "]"); System.out.println("watch-notify deleted " + rootPath + " : " + name); } public void fileCreated(int wd, String rootPath, String name) { System.out.println("[" + dfFilesNotify.format(new Date()) + "]"); System.out.println("watch-notify created " + rootPath + " : " + name); } public void fileWrittenClosed(int watchId, String root, String name) { System.out.println("[" + dfFilesNotify.format(new Date()) + "]"); System.out.println("watch-notify-written watchId: " + watchId + ""); System.out.println("watch-notify-written root: " + root + ""); System.out.println("watch-notify-written name: " + name + ""); if ((root == null) || (name == null) || name.endsWith("swp") || name.endsWith("swx") || name.endsWith("~") || name.endsWith("swpx")) { return; } try { notifyKafka(root, name, "FILE_WRITTEN_CLOSED"); } catch (Exception e) { System.out.println("call FileParseService failed, " + e); } } public void fileNowrittenClosed(int watchId, String root, String name) { File file = new File(root + "/" + name); if (file.isDirectory()) { return; } System.out.println("[" + dfFilesNotify.format(new Date()) + "]"); System.out.println("watch-notify-nowritten watchId: " + watchId + ""); System.out.println("watch-notify-nowritten root: " + root + ""); System.out.println("watch-notify-nowritten name: " + name + ""); if ((root == null) || (name == null) || name.endsWith("swp") || name.endsWith("swx") || name.endsWith("~") || name.endsWith("swpx")) { return; } try { notifyKafka(root, name, "FILE_NOWRITTEN_CLOSED"); } catch (Exception e) { System.out.println("call FileParseService failed, " + e); } } private void notifyKafka(String directory, String fileName, String event) throws Exception { String propurl = notifyListenersMap.get(directory).get(event) + ""; File file = new File(propurl); if (!file.exists()) { return; } Map<String, String> map = new HashMap<String, String>(); map.put("fileTime", dfFilesNotify.format(new Date())); map.put("fileName", directory + "/" + fileName); map.put("fileEvent", event); map.put("sftpHostIp", InetAddress.getLocalHost().getHostAddress()); map.put("sftpHostName", InetAddress.getLocalHost().getHostName()); ObjectMapper mapper = new ObjectMapper(); String message = mapper.writeValueAsString(map); JDtechProducer producer = JDtechProducer.getInstance(propurl); producer.send(1 + "", message); System.out.println("watch-notify kafka send : \n" + message + "\n"); // producer.close(); }
同时对应适配器中也需要加上相应的事件
int linuxMask = 0; if ((mask & JNotify.FILE_CREATED) != 0) { linuxMask |= JNotify_linux.IN_CREATE; } if ((mask & JNotify.FILE_DELETED) != 0) { linuxMask |= JNotify_linux.IN_DELETE; linuxMask |= JNotify_linux.IN_DELETE_SELF; } if ((mask & JNotify.FILE_MODIFIED) != 0) { linuxMask |= JNotify_linux.IN_ATTRIB; linuxMask |= JNotify_linux.IN_MODIFY; } if ((mask & JNotify.FILE_RENAMED) != 0) { linuxMask |= JNotify_linux.IN_MOVED_FROM; linuxMask |= JNotify_linux.IN_MOVED_TO; } if ((mask & JNotify.FILE_WRITTEN_CLOSED) != 0) { linuxMask |= JNotify_linux.IN_CLOSE_WRITE; } if ((mask & JNotify.FILE_NOWRITTEN_CLOSED) != 0) { linuxMask |= JNotify_linux.IN_CLOSE_NOWRITE; }
PS:此处改进是否可以将操作的事件发起方记录下来?此为下一步的优化点。