jnotify 监听文件读写

linux环境下需要监听文件事件,jnotify给出java版的解决方案,引用inotify机制。 

应对不同事件我们只需要在对应平台下添加对应事件监听即可。 


此为监听文件创建、改变、删除、读、写等操作 

Java代码 
  1. public void fileRenamed(int wd, String rootPath, String oldName, String newName) {  
  2.     System.out.println("[" + dfFilesNotify.format(new Date()) + "]");  
  3.     System.out.println("watch-notify renamed " + rootPath + " : " + oldName + " ---> " + newName);  
  4.     if ((rootPath == null) || (newName == null) || newName.endsWith("swp") || newName.endsWith("swx")  
  5.             || newName.endsWith("~") || newName.endsWith("swpx")) {  
  6.         return;  
  7.     }  
  8.     try {  
  9.         notifyKafka(rootPath, newName, "FILE_RENAMED");  
  10.     } catch (Exception e) {  
  11.         System.out.println("call FileParseService failed, " + e);  
  12.     }  
  13. }  
  14.   
  15. public void fileModified(int wd, String rootPath, String name) {  
  16.     System.out.println("[" + dfFilesNotify.format(new Date()) + "]");  
  17.     System.out.println("watch-notify modified " + rootPath + " : " + name);  
  18.     try {  
  19.         notifyKafka(rootPath, name, "FILE_NOWRITTEN_CLOSED");  
  20.     } catch (Exception e) {  
  21.         // TODO Auto-generated catch block  
  22.         e.printStackTrace();  
  23.     }  
  24. }  
  25.   
  26. public void fileDeleted(int wd, String rootPath, String name) {  
  27.     System.out.println("[" + dfFilesNotify.format(new Date()) + "]");  
  28.     System.out.println("watch-notify deleted " + rootPath + " : " + name);  
  29. }  
  30.   
  31. public void fileCreated(int wd, String rootPath, String name) {  
  32.     System.out.println("[" + dfFilesNotify.format(new Date()) + "]");  
  33.     System.out.println("watch-notify created " + rootPath + " : " + name);  
  34. }  
  35.   
  36. public void fileWrittenClosed(int watchId, String root, String name) {  
  37.     System.out.println("[" + dfFilesNotify.format(new Date()) + "]");  
  38.     System.out.println("watch-notify-written watchId: " + watchId + "");  
  39.     System.out.println("watch-notify-written root: " + root + "");  
  40.     System.out.println("watch-notify-written name: " + name + "");  
  41.   
  42.     if ((root == null) || (name == null) || name.endsWith("swp") || name.endsWith("swx") || name.endsWith("~")  
  43.             || name.endsWith("swpx")) {  
  44.         return;  
  45.     }  
  46.   
  47.     try {  
  48.         notifyKafka(root, name, "FILE_WRITTEN_CLOSED");  
  49.     } catch (Exception e) {  
  50.         System.out.println("call FileParseService failed, " + e);  
  51.     }  
  52. }  
  53.   
  54. public void fileNowrittenClosed(int watchId, String root, String name) {  
  55.     File file = new File(root + "/" + name);  
  56.     if (file.isDirectory()) {  
  57.         return;  
  58.     }  
  59.     System.out.println("[" + dfFilesNotify.format(new Date()) + "]");  
  60.     System.out.println("watch-notify-nowritten watchId: " + watchId + "");  
  61.     System.out.println("watch-notify-nowritten root: " + root + "");  
  62.     System.out.println("watch-notify-nowritten name: " + name + "");  
  63.   
  64.     if ((root == null) || (name == null) || name.endsWith("swp") || name.endsWith("swx") || name.endsWith("~")  
  65.             || name.endsWith("swpx")) {  
  66.         return;  
  67.     }  
  68.   
  69.     try {  
  70.         notifyKafka(root, name, "FILE_NOWRITTEN_CLOSED");  
  71.     } catch (Exception e) {  
  72.         System.out.println("call FileParseService failed, " + e);  
  73.     }  
  74. }  
  75.   
  76. private void notifyKafka(String directory, String fileName, String event) throws Exception {  
  77.     String propurl = notifyListenersMap.get(directory).get(event) + "";  
  78.     File file = new File(propurl);  
  79.     if (!file.exists()) {  
  80.         return;  
  81.     }  
  82.     Map<String, String> map = new HashMap<String, String>();  
  83.     map.put("fileTime", dfFilesNotify.format(new Date()));  
  84.     map.put("fileName", directory + "/" + fileName);  
  85.     map.put("fileEvent", event);  
  86.     map.put("sftpHostIp", InetAddress.getLocalHost().getHostAddress());  
  87.     map.put("sftpHostName", InetAddress.getLocalHost().getHostName());  
  88.     ObjectMapper mapper = new ObjectMapper();  
  89.     String message = mapper.writeValueAsString(map);  
  90.   
  91.     JDtechProducer producer = JDtechProducer.getInstance(propurl);  
  92.     producer.send(1 + "", message);  
  93.     System.out.println("watch-notify kafka send : \n" + message + "\n");  
  94.     // producer.close();  
  95. }  





同时对应适配器中也需要加上相应的事件 

Java代码 
  1. int linuxMask = 0;  
  2. if ((mask & JNotify.FILE_CREATED) != 0) {  
  3.     linuxMask |= JNotify_linux.IN_CREATE;  
  4. }  
  5. if ((mask & JNotify.FILE_DELETED) != 0) {  
  6.     linuxMask |= JNotify_linux.IN_DELETE;  
  7.     linuxMask |= JNotify_linux.IN_DELETE_SELF;  
  8. }  
  9. if ((mask & JNotify.FILE_MODIFIED) != 0) {  
  10.     linuxMask |= JNotify_linux.IN_ATTRIB;  
  11.     linuxMask |= JNotify_linux.IN_MODIFY;  
  12. }  
  13. if ((mask & JNotify.FILE_RENAMED) != 0) {  
  14.     linuxMask |= JNotify_linux.IN_MOVED_FROM;  
  15.     linuxMask |= JNotify_linux.IN_MOVED_TO;  
  16. }  
  17. if ((mask & JNotify.FILE_WRITTEN_CLOSED) != 0) {  
  18.     linuxMask |= JNotify_linux.IN_CLOSE_WRITE;  
  19. }  
  20. if ((mask & JNotify.FILE_NOWRITTEN_CLOSED) != 0) {  
  21.     linuxMask |= JNotify_linux.IN_CLOSE_NOWRITE;  
  22. }  




PS:此处改进是否可以将操作的事件发起方记录下来?此为下一步的优化点。 

posted @ 2017-05-17 10:27  葫芦一生  阅读(1141)  评论(0编辑  收藏  举报