怎樣登陸wordpress短視頻seo詢盤獲客系統(tǒng)軟件
-
在文件的內(nèi)容發(fā)生變動(dòng)時(shí),應(yīng)用可以感知這種變種,并重新加載文件內(nèi)容,更新應(yīng)用內(nèi)部緩存
-
實(shí)現(xiàn)
-
輪詢:定時(shí)器Timer,ScheduledExecutorService
-
判斷文件修改:根據(jù)java.io.File#lastModified獲取文件的上次修改時(shí)間,比對(duì)
-
public class FileUpTest{private long lastTime;private void ttt(){throw new NullPointerException();}@Testpublic void testFileUpdate(){File file = new File("/tmp/alarmConfig");//首先文件的最近一次修改時(shí)間戳lastTime = file.lastModified();//定時(shí)任務(wù),每秒來(lái)判斷一下文件是否發(fā)生變動(dòng),即判斷l(xiāng)astModified是否改變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();}} }
-
使用這種的,如果定時(shí)任務(wù)執(zhí)行過(guò)程中遇到發(fā)生異常,則后面的任務(wù)將不再執(zhí)行
-
-
apache版本
-
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version> </dependency>
-
借助工具中FileAlterationObserver,FileAlterationListener,FileAlterationMonitor三個(gè)類實(shí)現(xiàn)相關(guān)需求
-
public class PropertiesConfListenerHelper{public static boolean registerConfChangeListener(File file,Function<File,Map<String,AlarmConfig>> func){try{//輪詢間隔5秒long interval = TimeUnit.SECONDS.toMillis(5);//因?yàn)楸O(jiān)聽是以目錄為單位進(jìn)行的,所以這里直接獲取文件的根目錄File dir = file.getParentFile();//創(chuàng)建一個(gè)文件觀察期用于過(guò)濾FileAlterationObserver observer = new FileAlterationObserver(dir,FileFilterUtils.and(FileFilterUtils.fileFileFilter(),FileFilterUtils.nameFileFilter(file.getName())));//設(shè)置文件變化監(jiān)聽器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 Function<File,Map<String,AlarmConfig>> func;public MyfileListener(Function<File,Map<String,AlarmConfig>> func){this.func = func;}@Overridepublic void onFileChange(File file){Map<String,AlarmConfig> ans = func.apply(file);//如果加載失敗,打印一條日志log.warn("PropertiesConfig changed ! reload ans: {}",ans);}} }
-
介紹
- 這個(gè)文件監(jiān)聽,是以目錄為根源,可以設(shè)置過(guò)濾器,來(lái)實(shí)現(xiàn)對(duì)應(yīng)文件變動(dòng)的監(jiān)聽
- 上面的registerConfChangeListener方法,傳入的file是具體的配置文件,因此構(gòu)建參數(shù)的時(shí)候,拿到目錄,拿到文件名作為過(guò)濾
- 第二個(gè)參數(shù)是jdk1.8語(yǔ)法,其中為具體的讀取配置文件內(nèi)容,并影射為對(duì)應(yīng)的實(shí)體對(duì)象
- 如果func方式執(zhí)行時(shí),拋出了一場(chǎng),程序失敗,不在運(yùn)行
-
-
JDK版本
-
JDK1.7提供了一個(gè)WatchService,可以用來(lái)實(shí)現(xiàn)文件變動(dòng)的監(jiān)聽
-
@Test public void testFileUpWather() throws IOException{//監(jiān)聽必須是目錄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){ // 實(shí)踐可能lost or discardedcontinue; }Path fileName = (Path)event.context();System.out.println("文件更新:"+fileName);}if(!key.reset()){//重設(shè)WatchKeybreak;}}}catch(Exception e){e.printStackTrace();}}).start();try{Thread.sleep(1000* 60 * 10);}catch(InterruptedException e){e.printStackTrace();} }
-
千萬(wàn)不要在定時(shí)任務(wù)或者文件變動(dòng)的回調(diào)方法中拋出異常
-
為了避免異常斷開情況,一個(gè)是可以做的實(shí)現(xiàn)借助EventBus的異步消息通知機(jī)制來(lái)實(shí)現(xiàn),當(dāng)文件變動(dòng)之后,發(fā)送一個(gè)消息即可,然后在具體的重新加載文件內(nèi)容的方法上,添加一個(gè)@Subscribe注解即可,這樣既實(shí)現(xiàn)了解耦,也避免了一場(chǎng)導(dǎo)致的服務(wù)異常
-