private void _add(Object pojo) { if (tempPath == null) tempPath = "data" + File.separator + key; if (tempFile == null) { tempFile = "" + UUID.randomUUID(); } // 创建一个lock文件 File lockFile = FileUtil.openFile(tempPath, tempFile + ".lock"); // 将文件加锁 FileLock fileLock = FileUtil.tryLockFile(lockFile); if (fileLock == null) { tempFile = "" + UUID.randomUUID(); lockFile = FileUtil.openFile(tempPath, tempFile + ".lock"); fileLock = FileUtil.tryLockFile(lockFile); } // 将temp文件打开 File file = FileUtil.openFile(tempPath, tempFile + ".data"); FileUtil.writeObjectToFile(pojo, file, true); // 存储完毕,将文件解锁 FileUtil.unlockFile(fileLock); objListSize++; }
private List<Object> _read(String oldFile, FileLock fileLock) { List<Object> temp1 = null; tempFile = "" + UUID.randomUUID(); File file = FileUtil.openFile(tempPath, oldFile + ".data"); if (fileLock != null) { temp1 = FileUtil.readObjectFromFile(file); } else { // 打印日志,输出,说明文件已经被打开,无法读取 } objListSize = 0; return temp1; }
private void _doBatchUpdate() { if (log.isDebugEnabled()) log.debug("Start Executing Dao,key=" + key + ",sql=" + sql); int[] result = null; long time0 = System.currentTimeMillis(); List<Object> temp1 = null; List<Object[]> temp2 = null; String oldFile = tempFile; FileLock fileLock = null; synchronized (objListLock) { if (objListSize > 0 || objList.size() > 0) { if (AsyncService.isFileCache) { File lockFile = FileUtil.openFile(tempPath, oldFile + ".lock"); fileLock = FileUtil.tryLockFile(lockFile); temp1 = _read(oldFile, fileLock); } else { temp1 = objList; objList = new ArrayList<Object>(); } } else if (paramArrayList.size() > 0) { temp2 = paramArrayList; paramArrayList = new ArrayList<Object[]>(); } } if (null != temp1) { result = batchUpdateByPojo(sql, temp1); if (log.isInfoEnabled()) { long timeTake = System.currentTimeMillis() - time0; if (timeTake > 1000) // 批量保存任务超过1秒打印信息 log.info("key=" + key + ",Success Count=" + result.length + ",Use Millisecond=" + timeTake); tracer.trace("key=" + key + ",batchUpdate takes(milliseconds):" + timeTake); } if (null != sqlAlt) { List<Object> listAlt = new ArrayList<Object>(); for (int i = 0; i < result.length; i++) { if (result[i] <= 0) { listAlt.add(temp1.get(i)); } } if (listAlt.size() > 0) batchUpdateByPojo(sqlAlt, listAlt); } lastIoTime = System.currentTimeMillis(); if (AsyncService.isFileCache) { FileUtil.deleteFile(tempPath, oldFile + ".data"); FileUtil.unlockFile(fileLock); FileUtil.deleteFile(tempPath, oldFile + ".lock"); } } else if (null != temp2) { result = batchUpdateByParams(sql, temp2); if (log.isInfoEnabled()) { long timeTake = System.currentTimeMillis() - time0; if (timeTake > 1000) // 批量保存任务超过1秒打印信息 log.info("key=" + key + ",Success Count=" + result.length + ",Use Millisecond=" + timeTake); tracer.trace("key=" + key + ",batchUpdate takes(milliseconds):" + timeTake); } if (null != sqlAlt) { List<Object[]> listAlt = new ArrayList<Object[]>(); for (int i = 0; i < result.length; i++) { if (result[i] <= 0) { listAlt.add(temp2.get(i)); } } if (listAlt.size() > 0) batchUpdateByParams(sqlAlt, listAlt); } lastIoTime = System.currentTimeMillis(); } }