福田网站建设龙岗网站建设龙岗网站建设龙岗网站建设,常州网站建设公司市场,制作充值网站,自己注册公司流程和费用多少在文件的内容发生变动时#xff0c;应用可以感知这种变种#xff0c;并重新加载文件内容#xff0c;更新应用内部缓存 实现 轮询#xff1a;定时器Timer#xff0c;ScheduledExecutorService 判断文件修改#xff1a;根据java.io.File#lastModified获取文件的上次修改时… 在文件的内容发生变动时应用可以感知这种变种并重新加载文件内容更新应用内部缓存 实现 轮询定时器TimerScheduledExecutorService 判断文件修改根据java.io.File#lastModified获取文件的上次修改时间比对 public class FileUpTest{private long lastTime;private void ttt(){throw new NullPointerException();}Testpublic void testFileUpdate(){File file new File(/tmp/alarmConfig);//首先文件的最近一次修改时间戳lastTime file.lastModified();//定时任务每秒来判断一下文件是否发生变动即判断lastModified是否改变ScheduledExecutorService scheduledExcutorService Executors.newScheduledThreadPool(1);scheduledExecutorService.scheduleAtFixedRate(new Runnable(){Oveerridepublic void run(){if(file.lastModified() lastTime){System.out.println(file update! time : file.lastModified());lastTime file.getlastModified();ttt();}}},0,1,TimeUnit.SECONDS);try{Thread.sleep(100*60);}catch(InterruptedException e){e.printStackTrace();}}
}使用这种的如果定时任务执行过程中遇到发生异常则后面的任务将不再执行 apache版本 dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.6/version
/dependency借助工具中FileAlterationObserver,FileAlterationListener,FileAlterationMonitor三个类实现相关需求 public class PropertiesConfListenerHelper{public static boolean registerConfChangeListener(File file,FunctionFile,MapString,AlarmConfig func){try{//轮询间隔5秒long interval TimeUnit.SECONDS.toMillis(5);//因为监听是以目录为单位进行的所以这里直接获取文件的根目录File dir file.getParentFile();//创建一个文件观察期用于过滤FileAlterationObserver observer new FileAlterationObserver(dir,FileFilterUtils.and(FileFilterUtils.fileFileFilter(),FileFilterUtils.nameFileFilter(file.getName())));//设置文件变化监听器observer.addListener(new MyFileListener(func));FileAlterationMonitor monitor new FileAlterationMonitor(interval,observer);monitor.start();return true;}catch(Exception e){log.error(register properties change listener error! e:{},e);return false;}}static final class MyFileListener extends FileAlterationListenerAdaptor{private FunctionFile,MapString,AlarmConfig func;public MyfileListener(FunctionFile,MapString,AlarmConfig func){this.func func;}Overridepublic void onFileChange(File file){MapString,AlarmConfig ans func.apply(file);//如果加载失败打印一条日志log.warn(PropertiesConfig changed ! reload ans: {},ans);}}
}介绍 这个文件监听是以目录为根源可以设置过滤器来实现对应文件变动的监听上面的registerConfChangeListener方法传入的file是具体的配置文件因此构建参数的时候拿到目录拿到文件名作为过滤第二个参数是jdk1.8语法其中为具体的读取配置文件内容并影射为对应的实体对象如果func方式执行时抛出了一场程序失败不在运行 JDK版本 JDK1.7提供了一个WatchService可以用来实现文件变动的监听 Test
public void testFileUpWather() throws IOException{//监听必须是目录Path path Paths.get(/tmp);WatchService watcher FileSystems.getDefault().newWatchService();path.register(watcher,ENTRY_MODIFY);new Thread(() -{try{while(true){WatchKey key watcher.take();for(WatchEvent? event : key.pollEvents()){if(event.kind() OVERFLOW){ // 实践可能lost or discardedcontinue }Path fileName (Path)event.context();System.out.println(文件更新fileName);}if(!key.reset()){//重设WatchKeybreak}}}catch(Exception e){e.printStackTrace();}}).start();try{Thread.sleep(1000* 60 * 10);}catch(InterruptedException e){e.printStackTrace();}
}千万不要在定时任务或者文件变动的回调方法中抛出异常 为了避免异常断开情况一个是可以做的实现借助EventBus的异步消息通知机制来实现当文件变动之后发送一个消息即可然后在具体的重新加载文件内容的方法上添加一个Subscribe注解即可这样既实现了解耦也避免了一场导致的服务异常