@Override
  public synchronized void addTail(final MessageReference ref, final boolean direct) {
    SimpleString prop = ref.getMessage().getSimpleStringProperty(Message.HDR_LAST_VALUE_NAME);

    if (prop != null) {
      HolderReference hr = map.get(prop);

      if (hr != null) {
        // We need to overwrite the old ref with the new one and ack the old one

        MessageReference oldRef = hr.getReference();

        referenceHandled();

        try {
          oldRef.acknowledge();
        } catch (Exception e) {
          ActiveMQServerLogger.LOGGER.errorAckingOldReference(e);
        }

        hr.setReference(ref);

      } else {
        hr = new HolderReference(prop, ref);

        map.put(prop, hr);

        super.addTail(hr, direct);
      }
    } else {
      super.addTail(ref, direct);
    }
  }
  @Override
  public synchronized void addHead(final MessageReference ref) {
    SimpleString prop = ref.getMessage().getSimpleStringProperty(Message.HDR_LAST_VALUE_NAME);

    if (prop != null) {
      HolderReference hr = map.get(prop);

      if (hr != null) {
        // We keep the current ref and ack the one we are returning

        super.referenceHandled();

        try {
          super.acknowledge(ref);
        } catch (Exception e) {
          ActiveMQServerLogger.LOGGER.errorAckingOldReference(e);
        }
      } else {
        map.put(prop, (HolderReference) ref);

        super.addHead(ref);
      }
    } else {
      super.addHead(ref);
    }
  }