/MessageSender.java Secret
Created
February 5, 2014 07:17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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