好吧,這是一個奇怪的。 第一次保存attampt通常有效(最多1次嘗試)。
但在一個很重的負載(許多保存連續)保存的文件消失。
如果取消對「的Thread.sleep」的錯誤都會被抓住,否則驗證通過成功地java保存的文件消失(ObjectOutputStream)
public void save(Object key, T objToSave) throws FileNotFoundException, IOException {
IOException ex = null;
for (int i = 0; i < NUM_OF_RETRIES; i++) {
try {
/* saving */
String filePath = getFilePath(key);
OutputStream outStream = getOutStream(filePath);
ObjectOutputStream os = new ObjectOutputStream(outStream);
os.writeObject(objToSave);
os.close();
/* validations warnings etc. */
if (i>0){
logger.warn(objToSave + " saved on attamped " + i);
/* sleep more on each fail */
Thread.sleep(100+i*8);
}
//Thread.sleep(50);
File doneFile = new File(filePath);
if (! (doneFile.exists())){
logger.error("got here but file was not witten to disk ! id was" + key);
throw new IOException();
}
logger.info("6. start persist " + key + " path=" + new File(filePath).getAbsolutePath() + " exists="+doneFile.exists());
return;
} catch (IOException e) {
logger.error(objToSave + " failed on attamped " + i);
ex = e;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
throw ex;
}
它並沒有消失。你只是重寫文件系統,以便通過「exists」注意到文件真的存在。 – 2012-07-20 10:34:15
謝謝,但我補充說,驗證只有在文件沒有保存在第一個地方(在目錄上運行「ls」)。隨着睡眠,我可以看到文件丟失,並再次寫入 – ozma 2012-07-20 10:40:23
是否有可能使用多個線程或進程寫入此文件? – 2012-07-20 11:54:54