import config.Config;

 import java.io.IOException;
import java.nio.file.*;
import java.util.List;
import java.util.concurrent.TimeUnit; /**
* Created by jason on 2017/2/14.
*/
public class WatchDirService {
private WatchService watchService;
private boolean notDone = true; public WatchDirService(String dirPath){
init(dirPath);
} private void init(String dirPath) {
Path path = Paths.get(dirPath);
try {
watchService = FileSystems.getDefault().newWatchService();
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_DELETE);
} catch (IOException e) {
e.printStackTrace();
}
} public void start(){
System.out.print("watch...");
while (notDone){
try {
WatchKey watchKey = watchService.poll(Config.POLL_TIME_OUT, TimeUnit.SECONDS);
System.out.print("change");
if(watchKey != null){
List<WatchEvent<?>> events = watchKey.pollEvents();
for (WatchEvent event : events){
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW){
continue;
}
WatchEvent<Path> ev = event;
Path path = ev.context();
if(kind == StandardWatchEventKinds.ENTRY_CREATE){
System.out.println("create " + path.getFileName());
}else if(kind == StandardWatchEventKinds.ENTRY_MODIFY){
System.out.println("modify " + path.getFileName());
}else if(kind == StandardWatchEventKinds.ENTRY_DELETE){
System.out.println("delete " + path.getFileName());
}
}
if(!watchKey.reset()){
System.out.println("exit watch server");
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}
}
}

WatchDirService 实时监控的更多相关文章

  1. 安装 log.io 实时监控 php_error 日志

    Log.io 实时监控 php_error.log 日志 开启 php_error 实时监控日志的第一步,要首先开启 php_error 的功能. vi php.ini 修改 PHP 配置文件,将 ; ...

  2. Android(Linux)实时监控串口数据

    之前在做WinCE车载方案时,曾做过一个小工具TraceMonitor,用于显示WinCE系统上应用程序的调试信息,特别是在实车调试时,用于监控和显示CAN盒与主机之间的串口数据.因为需要抢占市场先机 ...

  3. 项目-基于视频压缩的实时监控系统--tiny6410

    项目-基于视频压缩的实时监控系统--tiny6410 @国嵌linux学习笔记. 1. 构造服务端结构体 server struct server { int epfd; //保存epoll指针 st ...

  4. 实时监控log文件

    一个进程在运行,并在不断的写log,你需要实时监控log文件的更新(一般是debug时用),怎么办,不断的打开,关闭文件吗? 不用,至少有两个方法,来自两个很常用的命令: tail -f log.tx ...

  5. 利用ngxtop实时监控nginx的访问情况

    关于对nginx web server的实时访问的实时监控问题,我很久之前就想实现的,现在虽有nginx自带的status扩展,但那是全局的,无法细分到vhost,并且提供的metric也很少,加之目 ...

  6. OVM-V1.2 版发布,新增实时监控、支持一键升级

    OVM是国内首款.完全免费.企业级--混合虚拟化管理平台,OVM是从中小企业目前的困境得到启发,完全基于国内企业特点开发,更多的关注国内中小企业用户的产品需求. OVM-V1.2 该版本功能变动如下: ...

  7. Spotlight实时监控Windows Server 2008

    Windows Server 2008作为服务器平台已逐渐被推广和应用,丰富的功能和良好的稳定性为其赢得了不错的口碑.但是和Windows Server 2003相比,其系统的自我监控功能并没有多大的 ...

  8. 转: 透过CAT,来看分布式实时监控系统的设计与实现

    评注: 开源的分布式监控系统 转:http://www.infoq.com/cn/articles/distributed-real-time-monitoring-and-control-syste ...

  9. C#实现对文件目录的实时监控

    本文主要描述如何通过C#实现实时监控文件目录下的变化,包括文件和目录的添加,删除,修改和重命名等操作. 首先,我们需要对.net提供的FileSystemWatcher类有所了解.我有些懒,找了MSD ...

随机推荐

  1. 自定义InputFormat和OutputFormat案例

    一.自定义InputFormat InputFormat是输入流,在前面的例子中使用的是文件输入输出流FileInputFormat和FileOutputFormat,而FileInputFormat ...

  2. HihoCoder1338 A Game (区间DP)

    <题目链接> 题目大意: 两个人轮流从一个序列中取数,他们都面临同样的二选一决策:是拿走最左边的数,还是拿走最右边的数?问先手最多能够得到的分数是多少. 解题分析: 一道比较经典的DP,因 ...

  3. Phabricator服务的搭建

    目录 概述 遇到的坑-Phabricator连接不上MySQL 准备环境 安装软件 安装Nginx 安装MySQL 安装PHP 下载Phabricator 配置 配置nginx和php 配置phabr ...

  4. 2019-2-20C#开发中常用加密解密方法解析

    C#开发中常用加密解密方法解析 一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是 message-digest algorithm 5[|ˈmes ...

  5. Hashmap误区

    HashMap简介 HashMap 是一个散列表,它存储的内容是键值对(key-value)映射.HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.Ser ...

  6. Mybatis 通用 Mapper 和 Spring 集成

    依赖 正常情况下,在原有依赖基础上增加的 mapper-spring. <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spr ...

  7. 移动端click事件清除

    http://blog.csdn.net/zfy865628361/article/details/49512095

  8. Java逆向武器库_反编译工具

    1.反编译工具之_jd-gui 官网下载地址:http://java-decompiler.github.io/#jd-gui-download 使用: 下载后解压直接使用即可. jd-gui的优势是 ...

  9. [LeetCode] Most Profit Assigning Work 安排最大利润的工作

    We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...

  10. vue-router 之 keep-alive

    参考 https://www.jianshu.com/p/0b0222954483