JNotify使用实例与BUG修复

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;

/**
 * 文件目录监听
 */
public class FileDirMonitor extends Thread{
	/**
	 * 测试主程序
	 */
	public static void main(String[] args){
		FileDirMonitor fd = new FileDirMonitor();
		try{
			System.out.println("main:" + Thread.currentThread().getName());
			fd.sample();
		}catch(Exception e){
		}
	}

	/**
	 * 一个JNotify测试实例
	 * @throws Exception
	 */
	public void sample() throws Exception{
		try{
			System.out.println("sample:" + Thread.currentThread().getName());
			// 被监听目录
			String path = "E:/temp/jnotify01/";
			String path2 = "E:/temp/jnotify02/";

			// 被监听事件
			int mask = JNotify.FILE_CREATED 	// 文件创建
					| JNotify.FILE_DELETED		// 文件删除
					| JNotify.FILE_MODIFIED		// 文件修改
					| JNotify.FILE_RENAMED;		// 文件改名

			// 是否监听子目录
			boolean watchSubtree = true;

			// 添加监听
			int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
			int watchID2 = JNotify.addWatch(path2, mask, watchSubtree, new Listener());

			// 这里主线程需要休眠一段时间,否则则主线程结束的话,监听时间就会结束
			try{
				Thread.sleep(1000000);
			}catch(Exception e){
				// 休眠被唤醒时,该捕获异常,不该额外处理,继续后续操作
				System.err.println("主线程被打断");
			}finally{
				// 移除监听
				boolean res = JNotify.removeWatch(watchID);
				boolean res2 = JNotify.removeWatch(watchID2);
				if(!res){
					System.err.println(path + "监听目录移除监听失败");
				}
				
				if(!res2){
					System.err.println(path2 + "监听目录移除监听失败");
				}
	
			}
		}catch(Exception e){
			System.out.println("error:" + e.getMessage());
		}
	}
}

/**
 * 文件监听器
 */
class Listener extends Thread implements JNotifyListener{
	public Listener(){
		System.out.println("new thread:" + Thread.currentThread().getName());
	}

	/**
	 * 该方法监听文件改名事件
	 * @param wd		被监听目录ID
	 * @param rootPath	被监听目录
	 * @param oldName	被改名前文件名
	 * @param newName	被改名后文件名	
	 */
	public void fileRenamed(int wd, String rootPath, String oldName, String newName){
		System.out.println("rename thread:" + Thread.currentThread().getName());
		print("renamed " + rootPath + ":" + oldName + "->" + newName);
	}

	/**
	 * 该方法监听文件修改事件
	 * @param wd		被监听目录ID
	 * @param rootPath	被监听目录
	 * @param name		被修改文件名
	 */
	public void fileModified(int wd, String rootPath, String name){
		System.out.println("modified thread:" + Thread.currentThread().getName());
		print("modified " + rootPath + ":" + name);
	}
	
	/**
	 * 注意:JNotify存在一次文件修改,触发多次fileModified方法的BUG,
	 * 该方法可以用来修复一次文件修改可能会触发多个fileModified方法,
	 * 从而减少没有必要的资源重新加载。但是由于t变量是类内共享变量,所
	 * 以注意线程安全,尽量避免共用Listener导致错误
	 */
//	long t = 0;
//	public void fileModified(int wd, String rootPath, String name) {
//		File file = new File(rootPath, name);
//		if (t != file.lastModified()) {
//			print("modified " + rootPath + " : " + name + " : ");
//			t = file.lastModified();
//		}
//	}

	/**
	 * 该方法监听文件删除事件
	 * @param wd		被监听目录ID
	 * @param rootPath	被监听目录
	 * @param name		被删除文件名
	 */
	public void fileDeleted(int wd, String rootPath, String name){
		System.out.println("deleted thread:" + Thread.currentThread().getName());
		print("deleted " + rootPath + ":" + name);
	}

	/**
	 * 改方法监听文件创建事件
	 * @param wd		被监听目录ID
	 * @param rootPath	被监听目录
	 * @param name		被创建文件名
	 */
	public void fileCreated(int wd, String rootPath, String name){
		System.out.println("created thread:" + Thread.currentThread().getName());
		print("created " + rootPath + ":" + name);
	}

	/**
	 * 错误打印
	 * @param msg	错误信息
	 */
	void print(String msg){
		System.err.println(" " + msg);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值