public void close() {
   stopped.set(true);
   if (readerTask != null) {
     readerTask.interrupt();
   }
   meta.close();
 }
 private void commitMessage() {
   MemCachedMessage<T> msg = null;
   if ((msg = lastMsg.getAndSet(null)) != null) {
     meta.updateRead(msg.getPos(), msg.getFile());
     // 如果是文件的第一个消息,触发备份和删除老文件事件
     if (msg.isFirstMessageInFile()) {
       dataFile.archiveAndRemoveOldFile(msg.getFile());
     }
   }
 }
 private void checkDataFileOutputStream() {
   FileInputStream input = null;
   try {
     // 启动的时候,dataFileOutputStream为null,不管上次启动时候写入的文件满了没,
     // 为了简单起见,还是新建一个文件
     if (dataFileOutputStream == null) {
       String newDataFileName = dataFile.createStorageFile();
       dataFileOutputStream = new ObjectOutputStream(new FileOutputStream(newDataFileName));
       meta.updateWrite(newDataFileName);
       // 更新写入的偏移量
       fileMap.put(newDataFileName, new AtomicLong(0));
       objectOutputstreamTimes = 0;
     }
     input = new FileInputStream(meta.getFileWriting());
     // 如果达到配置文件指定的大小,切换文件
     if (input.available()
         >= DefaultFileQueueConfig.getInstance(configFile).getMaxDataFileSize()) {
       dataFileOutputStream.close();
       String newDataFileName = dataFile.createStorageFile();
       dataFileOutputStream = new ObjectOutputStream(new FileOutputStream(newDataFileName));
       // 清除切换前老文件的记录
       fileMap.remove(meta.getFileWriting());
       meta.updateWrite(newDataFileName);
       // 更新写入的偏移量
       fileMap.put(newDataFileName, new AtomicLong(0));
       objectOutputstreamTimes = 0;
     }
   } catch (Exception e) {
     log.error("Create data file failed.", e);
     throw new RuntimeException("create data file failed.", e);
   } finally {
     if (input != null) {
       try {
         input.close();
       } catch (IOException e) {
         log.error("Close temp inputstream for size cal failed.", e);
       }
     }
   }
 }
 private void loadMeta() {
   memCacheLoadPos = meta.getReadPos();
   memCacheFileReading = meta.getFileReading();
   if ("null".equalsIgnoreCase(memCacheFileReading) || StringUtils.isBlank(memCacheFileReading)) {
     memCacheFileReading = null;
   }
   String readFileFromDiskScan = dataFile.getStorageFileName();
   if (readFileFromDiskScan == null) {
     if (memCacheFileReading != null) {
       log.error(
           "meta file's fileReading: "
               + memCacheFileReading
               + " not equal to data file remaining: "
               + null);
     }
     memCacheFileReading = null;
     memCacheLoadPos = 0;
     return;
   }
   if (memCacheFileReading == null) {
     memCacheFileReading = readFileFromDiskScan;
     memCacheLoadPos = 0;
   } else if (!memCacheFileReading.equals(readFileFromDiskScan)) {
     log.error(
         "meta file fileReading: "
             + memCacheFileReading
             + " not equal to "
             + readFileFromDiskScan);
     File f = new File(memCacheFileReading);
     if (f.exists()) {
       dataFile.adjust(memCacheFileReading);
       return;
     }
     memCacheFileReading = readFileFromDiskScan;
     memCacheLoadPos = 0;
   }
   return;
 }
 private void write(T m) {
   try {
     if (m == null) {
       return;
     }
     objectOutputstreamTimes++;
     if (objectOutputstreamTimes == 2000) {
       dataFileOutputStream.reset();
       objectOutputstreamTimes = 1;
     }
     dataFileOutputStream.writeObject(m);
     dataFileOutputStream.flush();
     // 更新写入的偏移量
     fileMap.get(meta.getFileWriting()).incrementAndGet();
   } catch (Exception e) {
     log.error("write message failed", e);
     throw new RuntimeException("write message failed", e);
   }
 }