org.freedesktop.dbus.connections.SenderThread Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbus-java-core Show documentation
Show all versions of dbus-java-core Show documentation
Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/).
package org.freedesktop.dbus.connections;
import java.util.concurrent.LinkedBlockingQueue;
import org.freedesktop.dbus.messages.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SenderThread extends Thread {
private final Logger logger = LoggerFactory.getLogger(getClass());
private boolean terminate;
private final LinkedBlockingQueue outgoingQueue = new LinkedBlockingQueue<>();
private final AbstractConnection abstractConnection;
SenderThread(AbstractConnection _abstractConnection) {
abstractConnection = _abstractConnection;
setName("DBUS Sender Thread");
}
public void terminate() {
terminate = true;
interrupt();
}
public LinkedBlockingQueue getOutgoingQueue() {
return outgoingQueue;
}
@Override
public void run() {
Message m = null;
logger.trace("Monitoring outbound queue");
// block on the outbound queue and send from it
while (!terminate) {
try {
m = outgoingQueue.take();
if (m != null) {
abstractConnection.sendMessage(m);
m = null;
}
} catch (InterruptedException _ex) {
if (!terminate) { // if terminate is true, shutdown was requested, do not log that
logger.warn("Interrupted while waiting for a message to send", _ex);
}
}
}
logger.debug("Flushing outbound queue and quitting");
// flush the outbound queue before disconnect.
while (!outgoingQueue.isEmpty()) {
Message poll = outgoingQueue.poll();
if (poll != null) {
abstractConnection.sendMessage(outgoingQueue.poll());
} else {
break;
}
}
}
}