private SimpleMessageGroup buildMessageGroup(Object groupId, boolean raw) {
    Assert.notNull(groupId, "'groupId' must not be null");
    Object mgm = this.doRetrieve(MESSAGE_GROUP_KEY_PREFIX + groupId);
    if (mgm != null) {
      Assert.isInstanceOf(MessageGroupMetadata.class, mgm);
      MessageGroupMetadata messageGroupMetadata = (MessageGroupMetadata) mgm;
      ArrayList<Message<?>> messages = new ArrayList<Message<?>>();

      Iterator<UUID> messageIds = messageGroupMetadata.messageIdIterator();
      while (messageIds.hasNext()) {
        if (raw) {
          messages.add(this.getRawMessage(messageIds.next()));
        } else {
          messages.add(this.getMessage(messageIds.next()));
        }
      }

      SimpleMessageGroup messageGroup =
          new SimpleMessageGroup(
              messages,
              groupId,
              messageGroupMetadata.getTimestamp(),
              messageGroupMetadata.isComplete());
      messageGroup.setLastReleasedMessageSequenceNumber(
          messageGroupMetadata.getLastReleasedMessageSequenceNumber());
      return messageGroup;
    } else {
      return new SimpleMessageGroup(groupId);
    }
  }
  /** Remove the MessageGroup with the provided group ID. */
  public void removeMessageGroup(Object groupId) {
    Assert.notNull(groupId, "'groupId' must not be null");
    Object mgm = this.doRemove(MESSAGE_GROUP_KEY_PREFIX + groupId);
    if (mgm != null) {
      Assert.isInstanceOf(MessageGroupMetadata.class, mgm);
      MessageGroupMetadata messageGroupMetadata = (MessageGroupMetadata) mgm;

      Iterator<UUID> messageIds = messageGroupMetadata.messageIdIterator();
      while (messageIds.hasNext()) {
        this.removeMessage(messageIds.next());
      }
    }
  }
  public Message<?> pollMessageFromGroup(Object groupId) {
    Assert.notNull(groupId, "'groupId' must not be null");
    Object mgm = this.doRetrieve(MESSAGE_GROUP_KEY_PREFIX + groupId);
    if (mgm != null) {
      Assert.isInstanceOf(MessageGroupMetadata.class, mgm);
      MessageGroupMetadata messageGroupMetadata = (MessageGroupMetadata) mgm;

      UUID firstId = messageGroupMetadata.firstId();
      if (firstId != null) {
        messageGroupMetadata.remove(firstId);
        this.doStore(MESSAGE_GROUP_KEY_PREFIX + groupId, messageGroupMetadata);
        return this.removeMessage(firstId);
      }
    }
    return null;
  }