jnotify listens for file read and write

In the linux environment, you need to monitor file events. jnotify provides a java version of the solution, referring to the inotify mechanism.

To deal with different events, we only need to add corresponding event listeners under the corresponding platform.


This is to monitor file creation, change, deletion, read, write and other operations
		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();
		}




At the same time, the corresponding event also needs to be added to the corresponding adapter
		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: Can the improvement here record the event initiator of the operation? This is the next optimization point.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486096&siteId=291194637