简介Jnotiy, 支持动态监控(支持级联监控)文件夹和文件的jar包。在linux中,调用linux底层的jnotify服务。在windows中,需要添加附件的dll文件。
因为通用的Maven仓库中没有此Jar文件,pom.xml文件需要如下配置:
<dependency> <groupId>net.contentobjects.jnotify</groupId> <artifactId>jnotify</artifactId> <version>0.94</version> </dependency> <!-- 在central库中没有这个包,需要添加以下的repo --> <repositories> <repository> <id>bintray</id> <url>http://dl.bintray.com/typesafe/maven-releases/</url> </repository> </repositories>
使用
-
-
首先从jar包中解目录压出dll文件,并放到工程lib/目录下。比如
/project/lib/native_libraries/...
-
测试代码
-
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动态库 - 32位
*/
static final String NATIVE_LIBRARIES_32BIT = "/lib/native_libraries/32bits/";
/**
* jnotify动态库 - 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"));
// 直接调用Jnotify时, 会发生异常:java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path
// 这是由于Jnotify使用JNI技术来加载dll文件,如果在类路径下没有发现相应的文件,就会抛出此异常。
// 因此可以通过指定程序的启动参数: java -Djava.library.path=/path/to/dll,
// 或者是通过修改JVM运行时的系统变量的方式来指定dll文件的路径,如下:
// 判断系统是32bit还是64bit,决定调用对应的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("类路径加载完成");
// 监听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) {
}
}
}