最近接到個簡單的需求:實時監控資料夾,把新增的檔案傳到對應的ftp,要求監控的資料夾可實時配置,使用jline提供人機互動介面進行檔案的重傳.略一思考,watchservice可以實現實時監控需求.
1.系統啟動,載入配置檔案,為每個配置項提供乙個監控器,當有檔案新增,上傳到對應的ftp
2.系統啟動,監控配置檔案,當配置檔案內容發生變化,為每個新增的配置項提供監控器
3.watchservice監控會阻塞執行緒,使用執行緒池管理執行緒
4.由於檔案較大,需要考慮檔案還在傳輸程式就進行上傳和上傳是否成功的問題
貼出關鍵的方法**,僅供參考
@configurationpublic class poolconfig
}
path path = paths.get(configfilepath);//configfilepath是資料夾記錄配置檔案的上一次修改的時間與當前修改時間對比,防止觸發2次.watchservice watchservice = filesystems.getdefault().newwatchservice();
//監聽資料夾路徑下的檔案內容發生改變
path.register(watchservice, standardwatcheventkinds.entry_modify);
while (true) 發生內容變化", configfile);
configfilemodifiedtime = lastmodified;
adddirwatch(configfile);}}
}// 必須重置watchkey,否則無法監控下次變化
if (!wk.reset())
}
watchservice watchservice = filesystems.getdefault().newwatchservice();每次上傳成功需要記錄,防止重傳//監聽新增
paths.get(dirpath).register(watchservice, standardwatcheventkinds.entry_create);
while (true)
}if (!key.reset())
}
public static void waitforfiletransfer(string filepath,long waittime) while (len1 < len2);上傳前記錄檔案大小==上傳後獲取ftp檔案大小,上傳成功} catch (exception e)
}
public static booleangetftpfilesize(string host, int port,string username, string password, string remotepath, string filename,long size)try catch (exception e) 失敗,case:[{}]", remotepath, e);
}//遍歷
for (ftpfile file : ftp.listfiles()) ", filename);
if(size == file.getsize())
break;}}
} catch (exception e) ", e);
} finally
} catch (ioexception e) ]", e);}}
return flag;
}
android LinearLayout新增分隔線
linearlayout支援直接顯示分隔線。設定標籤的 android showdividers屬性可以再linearlayout的相應位置顯示分隔線。如果有多個linearlayout,顯示效果和在 linearlayout之間加分隔線是一樣的。android showdividers屬性可以設定...
prometheus grafana監控新增新節點
已部署好prometheus grafana,並能正常執行 在client端先部署好已經編寫好的node exporter 編寫指令碼並執行 bin bash wget http ip node.tar.gz 獲取編寫好的node tar zxvf node.tar.gz c usr local 解...
python函式(往字典格式的txt檔案新增內容)
函式是組織好的,可重複使用的,用來實現單一,或相關聯功能的 段。函式能提高應用的模組性,和 的重複利用率。你已經知道python提供了許多內建函式,比如print 但你也可以自己建立函式,這被叫做使用者自定義函式。定義乙個函式 你可以定義乙個由自己想要功能的函式,以下是簡單的規則 menu menu...