Jnotify Note
簡(jiǎn)介
Jnotiy, 支持動(dòng)態(tài)監(jiān)控(支持級(jí)聯(lián)監(jiān)控)文件夾和文件的jar包递鹉。
在linux中弓候,調(diào)用linux底層的jnotify服務(wù)郎哭。
在windows中,需要添加附件的dll文件菇存。
下載安裝(Mavne)
<dependency>
<groupId>net.contentobjects.jnotify</groupId>
<artifactId>jnotify</artifactId>
<version>0.94</version>
</dependency>
<!-- 在central庫中沒有這個(gè)包夸研,需要添加以下的repo -->
<repositories>
<repository>
<id>bintray</id>
<url>http://dl.bintray.com/typesafe/maven-releases/</url>
</repository>
</repositories>
使用
首先從jar包中解目錄壓出dll文件,并放到工程lib/目錄下。比如
/project/lib/native_libraries/...
測(cè)試代碼
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Properties;
import org.apache.log4j.Logger;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;
public class TestJnotify {
static Logger log = Logger.getLogger(TestJnotify.class);
/**
* jnotify動(dòng)態(tài)庫 - 32位
*/
static final String NATIVE_LIBRARIES_32BIT = "/lib/native_libraries/32bits/";
/**
* jnotify動(dòng)態(tài)庫 - 64位
*/
static final String NATIVE_LIBRARIES_64BIT = "/lib/native_libraries/64bits/";
public static void main(String[] args) throws JNotifyException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
log.debug("-----------Jnotify test ---------");
Properties sysProps = System.getProperties();
String osArch = (String) sysProps.get("os.arch");
String osName = (String) sysProps.get("os.name");
String userDir = (String) sysProps.getProperty("user.dir");
LOG.debug("os.arch: " + osArch);
LOG.debug("os.name: " + osName);
LOG.debug("userDir: " + userDir);
LOG.debug("java.class.path: " + sysProps.get("java.class.path"));
// 直接調(diào)用Jnotify時(shí)依鸥, 會(huì)發(fā)生異常:java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path
// 這是由于Jnotify使用JNI技術(shù)來加載dll文件亥至,如果在類路徑下沒有發(fā)現(xiàn)相應(yīng)的文件,就會(huì)拋出此異常贱迟。
// 因此可以通過指定程序的啟動(dòng)參數(shù): java -Djava.library.path=/path/to/dll姐扮,
// 或者是通過修改JVM運(yùn)行時(shí)的系統(tǒng)變量的方式來指定dll文件的路徑,如下:
// 判斷系統(tǒng)是32bit還是64bit衣吠,決定調(diào)用對(duì)應(yīng)的dll文件
String jnotifyDir = NATIVE_LIBRARIES_64BIT;
if (!osArch.contains("64")) {
jnotifyDir = NATIVE_LIBRARIES_32BIT;
}
LOG.debug("jnotifyDir: " + jnotifyDir);
// 獲取目錄路徑
String pathToAdd = userDir + jnotifyDir ;
boolean isAdded = false;
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
final String[] paths = (String[]) usrPathsField.get(null);
LOG.debug("usr_paths: " + Arrays.toString(paths));
for (String path : paths) {
if (path.equals(pathToAdd)) {
isAdded = true;
break;
}
}
if (!isAdded) {
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length - 1] = pathToAdd;
usrPathsField.set(null, newPaths);
}
LOG.debug("java.library.path: " + System.getProperty("java.library.path"));
LOG.debug("usr_paths: " + Arrays.toString((String[]) usrPathsField.get(null)));
usrPathsField.setAccessible(false);
LOG.debug("類路徑加載完成");
// 監(jiān)聽F盤下的文件事件
JNotify.addWatch("F:\\", JNotify.FILE_ANY, true, new JNotifyListener() {
@Override
public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
log.debug("wd = " + wd + ", rootPath = " + rootPath);
log.debug("oldName = " + oldName + ", newName = " + newName);
}
@Override
public void fileModified(int wd, String rootPath, String fileName) {
log.debug("fileModified");
}
@Override
public void fileDeleted(int wd, String rootPath, String fileName) {
log.debug("fileDeleted");
}
@Override
public void fileCreated(int wd, String rootPath, String fileName) {
log.debug("fileDeleted");
}
});
while (true) {
}
}
}