@Override
 public synchronized void addMessageFirst(MessageReference node) throws Exception {
   batchList.addMessageFirst(node);
   size++;
   node.incrementReferenceCount();
   // this.messageSize.addSize(node.getMessage().getSize());
 }
  @Override
  public synchronized boolean tryAddMessageLast(MessageReference node, long maxWaitTime)
      throws Exception {
    if (!node.isExpired()) {
      try {
        regionDestination = (Destination) node.getMessage().getRegionDestination();
        if (isDiskListEmpty()) {
          if (hasSpace() || this.store == null) {
            memoryList.addMessageLast(node);
            node.incrementReferenceCount();
            setCacheEnabled(true);
            return true;
          }
        }
        if (!hasSpace()) {
          if (isDiskListEmpty()) {
            expireOldMessages();
            if (hasSpace()) {
              memoryList.addMessageLast(node);
              node.incrementReferenceCount();
              return true;
            } else {
              flushToDisk();
            }
          }
        }
        if (systemUsage.getTempUsage().waitForSpace(maxWaitTime)) {
          ByteSequence bs = getByteSequence(node.getMessage());
          getDiskList().addLast(node.getMessageId().toString(), bs);
          return true;
        }
        return false;

      } catch (Exception e) {
        LOG.error(
            "Caught an Exception adding a message: {} first to FilePendingMessageCursor ", node, e);
        throw new RuntimeException(e);
      }
    } else {
      discardExpiredMessage(node);
    }
    // message expired
    return true;
  }
 /** @return the next pending message */
 @Override
 public synchronized MessageReference next() {
   MessageReference reference = iter.next();
   last = reference;
   if (!isDiskListEmpty()) {
     // got from disk
     reference.getMessage().setRegionDestination(regionDestination);
     reference.getMessage().setMemoryUsage(this.getSystemUsage().getMemoryUsage());
   }
   reference.incrementReferenceCount();
   return reference;
 }
  /**
   * add message to await dispatch
   *
   * @param node
   */
  @Override
  public synchronized void addMessageFirst(MessageReference node) {
    if (!node.isExpired()) {
      try {
        regionDestination = (Destination) node.getMessage().getRegionDestination();
        if (isDiskListEmpty()) {
          if (hasSpace()) {
            memoryList.addMessageFirst(node);
            node.incrementReferenceCount();
            setCacheEnabled(true);
            return;
          }
        }
        if (!hasSpace()) {
          if (isDiskListEmpty()) {
            expireOldMessages();
            if (hasSpace()) {
              memoryList.addMessageFirst(node);
              node.incrementReferenceCount();
              return;
            } else {
              flushToDisk();
            }
          }
        }
        systemUsage.getTempUsage().waitForSpace();
        node.decrementReferenceCount();
        ByteSequence bs = getByteSequence(node.getMessage());
        Object locator = getDiskList().addFirst(node.getMessageId().toString(), bs);
        node.getMessageId().setPlistLocator(locator);

      } catch (Exception e) {
        LOG.error(
            "Caught an Exception adding a message: {} first to FilePendingMessageCursor ", node, e);
        throw new RuntimeException(e);
      }
    } else {
      discardExpiredMessage(node);
    }
  }
 @Override
 public synchronized LinkedList<MessageReference> pageInList(int maxItems) {
   LinkedList<MessageReference> result = new LinkedList<MessageReference>();
   int count = 0;
   for (Iterator<MessageReference> i = memoryList.iterator(); i.hasNext() && count < maxItems; ) {
     MessageReference ref = i.next();
     ref.incrementReferenceCount();
     result.add(ref);
     count++;
   }
   if (count < maxItems && !isDiskListEmpty()) {
     for (Iterator<MessageReference> i = new DiskIterator(); i.hasNext() && count < maxItems; ) {
       Message message = (Message) i.next();
       message.setRegionDestination(regionDestination);
       message.setMemoryUsage(this.getSystemUsage().getMemoryUsage());
       message.incrementReferenceCount();
       result.add(message);
       count++;
     }
   }
   return result;
 }