mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-26 08:12:05 +01:00
Improve OfflineMessages.getMessages()
poll the result before waiting for a message (with nextResult()), in order to prevent waiting unnecessarily. Add PacketCollector.pollResultOrThrow()
This commit is contained in:
parent
5dd97a363c
commit
187e158260
2 changed files with 30 additions and 0 deletions
|
@ -112,6 +112,25 @@ public class PacketCollector {
|
||||||
return (P) resultQueue.poll();
|
return (P) resultQueue.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polls to see if a packet is currently available and returns it, or
|
||||||
|
* immediately returns <tt>null</tt> if no packets are currently in the
|
||||||
|
* result queue.
|
||||||
|
* <p>
|
||||||
|
* Throws an XMPPErrorException in case the polled stanzas did contain an XMPPError.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return the next available packet.
|
||||||
|
* @throws XMPPErrorException in case an error response.
|
||||||
|
*/
|
||||||
|
public <P extends Packet> P pollResultOrThrow() throws XMPPErrorException {
|
||||||
|
P result = pollResult();
|
||||||
|
if (result != null) {
|
||||||
|
XMPPErrorException.ifHasErrorThenThrow(result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next available packet. The method call will block (not return) until a packet is
|
* Returns the next available packet. The method call will block (not return) until a packet is
|
||||||
* available.
|
* available.
|
||||||
|
|
|
@ -194,6 +194,17 @@ public class OfflineMessageManager {
|
||||||
Message message = messageCollector.nextResult();
|
Message message = messageCollector.nextResult();
|
||||||
while (message != null) {
|
while (message != null) {
|
||||||
messages.add(message);
|
messages.add(message);
|
||||||
|
// It is important that we query the resultCollector before the messageCollector
|
||||||
|
Packet result = resultCollector.pollResultOrThrow();
|
||||||
|
message = messageCollector.pollResult();
|
||||||
|
if (message == null && result != null) {
|
||||||
|
// No new messages, but we have a non-error IQ response, we are done
|
||||||
|
return messages;
|
||||||
|
} else if (message != null) {
|
||||||
|
// We have received a message without waiting, great, continue to add this message and proceed with
|
||||||
|
// the loop
|
||||||
|
continue;
|
||||||
|
}
|
||||||
message = messageCollector.nextResult();
|
message = messageCollector.nextResult();
|
||||||
}
|
}
|
||||||
resultCollector.nextResultOrThrow();
|
resultCollector.nextResultOrThrow();
|
||||||
|
|
Loading…
Reference in a new issue