java7 watchservice_Learning Java7 WatchService

WatchService 直接映射到 native file event notification mechanism,如果原生文件时间通知机制不可用,默认实现就会使用 polling 方式。

1. JDK的实现风格,java.nio.file.Path接口对应一个java.nio.file.Paths静态方法工具类。Path接口定义了register()方法,接受WatchService object作为第一个参数,一个WatchEvent.Kind类型的可变参数作为第二个参数。总共有 4 种events,ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW。OVERFLOW表示 events may been lost or discarded。如下创建并使用WatchService,

import static java.nio.file.StandardWatchEventKinds.*;

Path path = Paths.get("/home");

WatchService watchService = FileSystems.getDefault().newWatchService();

WatchKey watchKey = path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);

2. Path.register()返回一个WatchKey对象。有3种方式可使WatchKey的状态变为invalid,一是调用Watchkey.cancel(),二是被监控的目录不再有效,三是关闭WatchService object。有2种方式从队列中得到WatchKey,一是调用WatchService.poll(),

WatchKey watchKey = watchService.poll(60,TimeUnit.SECONDS);

二是调用WatchService.take(),

WatchKey key = watcherService.take();

3. 处理事件

public void handleEvents() throws InterruptedException

{

while (true)

{

WatchKey key = watcher.take();

for (WatchEvent> event : key.pollEvents())

{

Kind> kind = event.kind();

if (kind == OVERFLOW)

{// 事件可能lost or discarded

continue;

}

Path fileName = (Path) event.context();

System.out.printf("Event %s has happened,which fileName is %s%n", kind.name(), fileName);

}

if (!key.reset())

{

break;

}

}

}

代码中有一处调用了reset()方法,reset()方法把WatchKey state置回'ready'状态。如果有pending event,就立即re-queued WatchKey,如果没有,就保持ready state,知道新事件到来。

4. 一些问题,

a. WatchService被监控目录的子目录事件(重命名只会引发 ENTRY_MODIFY,而根目录会触发 ENTRY_DELETE 和 ENTRY_CREATE ),

b. 我们必须poll the WatchSercie for events,而不能接受asynchronous notification。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值