1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-08 20:11:08 +01:00

Synchronize PacketWriter shutdown

try the best to send the queue elements and the closing stream element.
This commit is contained in:
Florian Schmaus 2014-05-05 17:45:40 +02:00
parent 74d188aabb
commit 07d6b9203c
2 changed files with 30 additions and 3 deletions

View file

@ -23,6 +23,8 @@ import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
@ -48,6 +50,8 @@ class PacketWriter {
volatile boolean done;
AtomicBoolean shutdownDone = new AtomicBoolean(false);
/**
* Creates a new packet writer with the specified connection.
*
@ -65,6 +69,7 @@ class PacketWriter {
protected void init() {
this.writer = connection.writer;
done = false;
shutdownDone.set(false);
queue.start();
writerThread = new Thread() {
@ -115,6 +120,16 @@ class PacketWriter {
public void shutdown() {
done = true;
queue.shutdown();
synchronized(shutdownDone) {
if (!shutdownDone.get()) {
try {
shutdownDone.wait(connection.getPacketReplyTimeout());
}
catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "shutdown", e);
}
}
}
}
/**
@ -163,7 +178,7 @@ class PacketWriter {
writer.flush();
}
catch (Exception e) {
LOGGER.warning("Error flushing queue during shutdown, ignore and continue");
LOGGER.log(Level.WARNING, "Exception flushing queue during shutdown, ignore and continue", e);
}
// Delete the queue contents (hopefully nothing is left).
@ -175,7 +190,8 @@ class PacketWriter {
writer.flush();
}
catch (Exception e) {
// Do nothing
LOGGER.log(Level.WARNING, "Exception writing closing stream element", e);
}
finally {
try {
@ -185,6 +201,11 @@ class PacketWriter {
// Do nothing
}
}
shutdownDone.set(true);
synchronized(shutdownDone) {
shutdownDone.notify();
}
}
catch (IOException ioe) {
// The exception can be ignored if the the connection is 'done'
@ -213,4 +234,5 @@ class PacketWriter {
writer.write(stream.toString());
writer.flush();
}
}