Bad performance of Flexible Offline Messages

Smack version: 4.2.0 and 4.2.1

I have the following code:

    OfflineMessageManager offlineManager = new OfflineMessageManager(connection);
    List<OfflineMessageHeader> headers = offlineManager.getHeaders();

    List<String> nodes = new ArrayList<>();
    for (OfflineMessageHeader header : headers) {
        nodes.add(header.getStamp());

        Log.d(TAG, "GetMessages");
        List<Message> messages = offlineManager.getMessages(nodes);
        Log.d(TAG, "GetMessages.finish");
        
        processMessages(messages);
        nodes.clear();
    }

We have noticed, that it has a very bad performance and after debugging, I see that in the logs, time between
"GetMessages" and “GetMessages.finish” is always around 10 seconds.
After going into the details, We have found out the problem in the method org.jivesoftware.smackx.offline.OfflineMessageManager#getMessages(java.util.List<java.lang.String>), in the following part of the code:

        int pendingNodes = nodes.size();
        StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter);
        try {
            connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
            // Collect the received offline messages
            Message message = messageCollector.nextResult();
            while (message != null && pendingNodes > 0) {
                pendingNodes--;
                messages.add(message);
                message = messageCollector.nextResult();
            }
        }

The problem here is that when we get request to fetch 1 message,
We get the message, then we have:
message != null && pendingNodes = 1,
So we go into the loop, wait for the next message (Which will never come),
messageCollector will fail with timeout (10 seconds in our case! which is causing a delay) and after this will quit the loop and return the fetched message back.

So the bug is the wrong handling of pendingNodes.
To fix it there should be:

Message message = messageCollector.nextResult();
pendingNodes--;// <==== Add this line.

So, could you, please, create the ticket and apply the fix?

2 Likes

Thanks for reporting this. I’ve created SMACK-785.