From 52bd680bb573f8d94cd2eaa2d21e694c82f6d1a6 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 18 Nov 2017 15:49:57 +0100 Subject: [PATCH] Fix OfflineMessageManager.getMessages(List) by implementing a new logic how the messages are retrieved. Previously in case the node list has exactly one item, the method would perform an unnecessary call to nextResult() causing a delay. Fixes SMACK-785. --- .../smackx/offline/OfflineMessageManager.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java index 2529004f7..cd01eea76 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java @@ -19,6 +19,8 @@ package org.jivesoftware.smackx.offline; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; @@ -58,6 +60,8 @@ import org.jivesoftware.smackx.xdata.Form; */ public class OfflineMessageManager { + private static final Logger LOGGER = Logger.getLogger(OfflineMessageManager.class.getName()); + private final static String namespace = "http://jabber.org/protocol/offline"; private final XMPPConnection connection; @@ -144,7 +148,7 @@ public class OfflineMessageManager { * @throws InterruptedException */ public List getMessages(final List nodes) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { - List messages = new ArrayList(); + List messages = new ArrayList(nodes.size()); OfflineMessageRequest request = new OfflineMessageRequest(); for (String node : nodes) { OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node); @@ -165,12 +169,17 @@ public class OfflineMessageManager { try { connection.createStanzaCollectorAndSend(request).nextResultOrThrow(); // Collect the received offline messages - Message message = messageCollector.nextResult(); - while (message != null && pendingNodes > 0) { - pendingNodes--; - messages.add(message); + Message message; + do { message = messageCollector.nextResult(); - } + if (message != null) { + messages.add(message); + pendingNodes--; + } else if (message == null && pendingNodes > 0) { + LOGGER.log(Level.WARNING, + "Did not receive all expected offline messages. " + pendingNodes + " are missing."); + } + } while (message != null && pendingNodes > 0); } finally { // Stop queuing offline messages