현재 내가 필요로하는 특정 디렉토리 및 하위 디렉토리의 파일 변경 사항을 가로채는 데 필요한 요구 사항을 평가하려고합니다. 내 요구 사항에 적합한 몇 가지 도구는 jpathwatch와 jnotify입니다. jpathwatch는 작동하지만 재귀적인 디렉토리 감시를 지원하지 않습니다. Jnotify는 그 한계를 꽤 잘 처리 한 것으로 보입니다.새 파일에서 jNotify의 이상한 동작
jnotify를 평가하는 동안 나는 이상한 행동을 관찰했습니다. 이것은 리눅스와 윈도우 모두에서 일관성이있다. 예를 들어 설명해 보겠습니다. 다음과 같은 디렉토리 구조를 가지고 있습니다 :
C:
|--> Temp |
| --> File |
| --> Dir |
| --> SubDir
jNotify는 C :/Temp/File을 수신하도록 구성됩니다. 이제 "Dir"또는 "SubDir"폴더 아래에 파일을 놓으면 새로 만든 이벤트를 캡처합니다. 그러나 그와 동시에 3 개의 파일 수정 이벤트가 발생합니다. 다음은 "SubDir"폴더에 Extraction.log 파일을 새로 추가 할 때의 출력입니다.
created C:/Temp/File/Dir/SubDir/Extraction.log
modified C:/Temp/File/Dir/SubDir/Extraction.log
modified C:/Temp/File/Dir/SubDir/Extraction.log
modified C:/Temp/File/Dir/SubDir/Extraction.log
보시다시피, 파일 수정에는 3 가지 이벤트가 있습니다. 응용 프로그램에는 수정 또는 새 파일 작성 여부를 알 수있는 방법이 없습니다. 따라서 동일한 파일을 네 번 처리합니다.
JNotify 웹 사이트에 표시된 샘플 코드를 사용하고 있습니다.
package com.test.io;
import net.contentobjects.jnotify.JNotify;
public class JNotifyTest {
/**
* @param args
*/
public static void main(String[] args) {
JNotifyTest jNotify = new JNotifyTest();
try {
jNotify.sample();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sample() throws Exception {
// path to watch
String path = "C:\\Temp\\File\\";
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED |
JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
System.out.println("renamed " + rootPath + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
System.out.println("modified " + rootPath + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
System.out.println("deleted " + rootPath + name);
}
public void fileCreated(int wd, String rootPath, String name) {
System.out.println("created " + rootPath + name);
}
}
}
불행히도 1.6에서 juck 1.7을 사용할 수 없어서 jdk 1.7을 사용할 수 없습니다.
필자는 jNotify와 관련된 게시물을 거의 보지 못했지만 누군가가이 요구 사항을 해결하기위한 포인터 나 다른 솔루션을 제공 할 수 있다면 정말 고마워 할 것입니다. 이것은 대부분의 아마 jNotify의 문제가 아니라 방식 때문에 OS가 파일 생성을 처리
감사
답장을 보내 주셔서 감사합니다. 언급했듯이 파일 생성 또는 수정시 여러 이벤트를 발생시키는 OS 문제 일 수 있습니다.하지만 여러 파일이 연속적으로 처리되는 경우 특별히 제안한 버퍼링 솔루션이 어떻게 작동하는지 잘 모르겠습니다. – Shamik