protected synchronized void expireOldMessages() {
   if (!memoryList.isEmpty()) {
     for (Iterator<MessageReference> iterator = memoryList.iterator(); iterator.hasNext(); ) {
       MessageReference node = iterator.next();
       if (node.isExpired()) {
         node.decrementReferenceCount();
         discardExpiredMessage(node);
         iterator.remove();
       }
     }
   }
 }
  @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;
  }
  /**
   * 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);
    }
  }