Make PacketCollector handle InterruptedException

Fixes SMACK-602
This commit is contained in:
Florian Schmaus 2014-08-29 18:06:22 +02:00
parent b5b134f569
commit 72557dd354
1 changed files with 26 additions and 12 deletions

View File

@ -19,6 +19,8 @@ package org.jivesoftware.smack;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
@ -42,6 +44,8 @@ import org.jivesoftware.smack.packet.XMPPError;
*/
public class PacketCollector {
private static final Logger LOGGER = Logger.getLogger(PacketCollector.class.getName());
private PacketFilter packetFilter;
private ArrayBlockingQueue<Packet> resultQueue;
private XMPPConnection connection;
@ -114,12 +118,16 @@ public class PacketCollector {
* @return the next available packet.
*/
public Packet nextResultBlockForever() {
try {
return resultQueue.take();
}
catch (InterruptedException e) {
throw new RuntimeException(e);
Packet res = null;
while (res == null) {
try {
res = resultQueue.take();
} catch (InterruptedException e) {
LOGGER.log(Level.FINE,
"nextResultBlockForever was interrupted", e);
}
}
return res;
}
/**
@ -139,13 +147,19 @@ public class PacketCollector {
*
* @return the next available packet.
*/
public Packet nextResult(long timeout) {
try {
return resultQueue.poll(timeout, TimeUnit.MILLISECONDS);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
public Packet nextResult(final long timeout) {
Packet res = null;
long remainingWait = timeout;
final long waitStart = System.currentTimeMillis();
while (res == null && remainingWait > 0) {
try {
res = resultQueue.poll(remainingWait, TimeUnit.MILLISECONDS);
remainingWait = timeout - (System.currentTimeMillis() - waitStart);
} catch (InterruptedException e) {
LOGGER.log(Level.FINE, "nextResult was interrupted", e);
}
}
return res;
}
/**