code: public List<Message> getMessages() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
          OfflineMessageRequest request = new OfflineMessageRequest();
          request.setFetch(true);
  
          StanzaCollector resultCollector = connection.createStanzaCollectorAndSend(request);
          StanzaCollector.Configuration messageCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(PACKET_FILTER).setCollectorToReset(resultCollector);
  
          List<Message> messages;
          try (StanzaCollector messageCollector = connection.createStanzaCollector(messageCollectorConfiguration)) {
              resultCollector.nextResultOrThrow();
              // Be extra safe, cancel the message collector right here so that it does not collector
              // other messages that eventually match (although I've no idea how this could happen in
              // case of XEP-13).
              messageCollector.cancel();
              messages = new ArrayList<>(messageCollector.getCollectedCount());
              Message message;
              while ((message = messageCollector.pollResult()) != null) {
                  messages.add(message);
              }
          }
          return messages;
      }
docs: Returns a List of Messages with all the offline <tt>Messages</tt> of the user. The returned offline
  messages will not be deleted from the server. Use {@link #deleteMessages(java.util.List)}
  to delete the messages.
  
  @return a List with all the offline <tt>Messages</tt> of the user.
  @throws XMPPErrorException If the user is not allowed to make this request or the server does
  not support offline message retrieval.
  @throws NoResponseException if there was no response from the server.
  @throws NotConnectedException
  @throws InterruptedException