Skip to content

Instantly share code, notes, and snippets.

@JoelPM
Created February 5, 2014 07:17
public class MessageSender extends ConnectionRequiredRunnable {
private final MessageService.Client client;
private final BlockingQueue<Message> msgSendQueue;
public MessageSender(
TProtocol protocol,
ConnectionStatusMonitor connectionMonitor) {
super(connectionMonitor, "Message Sender");
this.client = new MessageService.Client(protocol);
this.msgSendQueue = new LinkedBlockingQueue<Message>();
}
public void send(Message msg) {
msgSendQueue.add(msg);
}
@Override
public void run() {
connectWait();
while (true) {
try {
Message msg = msgSendQueue.take();
try {
client.sendMessage(msg);
} catch (TException e) {
// The message isn't lost, but it could end up being sent out of
// order - not ideal.
msgSendQueue.add(msg);
disconnected();
}
} catch (InterruptedException e) {
// Thread will be interrupted if connection is lost, we should wait
// for reconnection if that happens.
connectWait();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment