zoukankan      html  css  js  c++  java
  • 借助JDK7中WatchService实现文件变更监听

    import org.springframework.core.io.FileSystemResource;
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    import java.io.IOException;
    import java.nio.file.*;
    import java.util.Objects;
    import java.util.Properties;
    
    
    /**
     * @author Created by niugang on 2019/10/16/19:41
     */
    @SuppressWarnings("InfiniteLoopStatement")
    public class WatchServiceDemo {
        private static String fileName="mail.properties";
        private static FileSystemResource classPathResource = null;
        private static WatchService watchService = null;
    
        public static Properties properties;
    
    
        static {
    
            String filePath = "D:/home/conf/mail.properties";
            //从文件系统加载文件
            classPathResource = new FileSystemResource(filePath);
            //监听filenam所在的目录下的文件修改、删除事件
            try {
                watchService = FileSystems.getDefault().newWatchService();
                Paths.get(classPathResource.getFile().getParent()).register(watchService,
                        StandardWatchEventKinds.ENTRY_MODIFY,
                        StandardWatchEventKinds.ENTRY_DELETE
                );
                properties = PropertiesLoaderUtils.loadProperties(classPathResource);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
            Thread watchThread = new Thread() {
                @Override
                public void run() {
    
                    while (true) {
                        WatchKey watchKey = null;
                        try {
                            watchKey = watchService.take();
                            for (WatchEvent<?> event : watchKey.pollEvents()) {
                                if (Objects.equals(event.context().toString(), fileName)) {
                                    properties = PropertiesLoaderUtils.loadProperties(classPathResource);
                                    break;
                                }
                            }
                            watchKey.reset();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (watchKey != null) {
                                watchKey.reset();
                            }
                        }
    
    
                    }
                }
            };
            //设置为守护线程
            watchThread.setDaemon(true);
            watchThread.start();
    
           //JVM停止时 关闭
            Runtime.getRuntime().addShutdownHook(new Thread(){
                @Override
                public void run() {
                    try {
                        watchService.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
    //
    //    public static void main(String[] args) throws InterruptedException {
    //        //验证  启动main方法   修改mail.properties文件 看控制台输出内容的变化
    //        while (true){
    //            System.out.println(properties);
    //            Thread.sleep(5000);
    //        }
    //
    //
    //
    //    }
    
    }
    
    

    微信公众号
    JAVA程序猿成长之路

  • 相关阅读:
    mysql导sql脚本
    oracle导sql脚本
    基于jdk proxy的动态代理模式
    vue组件之组件的生命周期
    vue组件之组件间的通信
    python-爬虫scrapy框架安装及基本使用
    mongdb的使用
    python-爬虫 多线程爬虫
    python-爬虫 爬虫利器BeautifulSoup
    python-爬虫lxml库
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12187009.html
Copyright © 2011-2022 走看看