Watchable — 감시 가능 객체 인터페이스

Watchable — 감시 가능 객체 인터페이스

감시 서비스(watch service)에 등록해서 변경 사항과 이벤트를 감시받을 수 있는 객체를 나타내는 인터페이스예요. 이 인터페이스는 register 메서드를 정의해 객체를 WatchService에 등록하고, 등록을 나타내는 WatchKey를 반환해요. 객체는 여러 감시 서비스에 등록할 수 있어요. 감시 서비스에 대한 등록은 키의 cancel 메서드를 호출해 취소해요.

출처: Java API Reference

본문

Path가 이 인터페이스를 구현하므로, 디렉터리를 WatchService에 등록해 그 항목들을 감시할 수 있어요.

  • WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws IOException — 이 객체를 감시 서비스에 등록해요. 감시할 이벤트 종류(ENTRY_CREATE 등)를 events로 지정해요.
  • WatchKey register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers) throws IOException — 이 객체를 감시 서비스에 등록하되, 감시 이벤트 종류 외에 수정자(modifiers)를 지정해요.
Path dir = Paths.get(".");
WatchKey key = dir.register(watcher,
    StandardWatchEventKinds.ENTRY_CREATE,
    StandardWatchEventKinds.ENTRY_MODIFY);

더 알아보기 (Learn more)