private final SortedMap Integer Request sendingQueue Collections synch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private final SortedMap<Integer, Request> sendingQueue = Collections.synchronizedSortedMap(new TreeMap<Integer, Request>());
private void addToSendingQueue(Request request) {
Printer.printLine("Adding to QUEUE '" + request.code + "' to " + name);
sendingQueue.put(request.code, request);
}
private void sendAllQueue() {
while (!sendingQueue.isEmpty()) {
Request request = sendingQueue.remove(sendingQueue.firstKey());
Printer.printLine("Sending from QUEUE '" + request.code + "' to " + name);
Printer.printLine(request.toString());
try {
connection.sendMessage(request.toString());
} catch (IOException e) {
Printer.exception("Error in sending from Queue", e);
}
}
}
@Override
public void send(Request request) {
if (connection != null) {
try {
if (!sendingQueue.isEmpty()) {
sendAllQueue();
}
Printer.printLine("Sending '" + request.code + "' to " + name);
Printer.printLine(request.toString());
connection.sendMessage(request.toString());
} catch (IOException ioe) {
Printer.exception(ID + " Error sending '" + request.code + "' to " + name, ioe);
addToSendingQueue(request);
}
} else {
addToSendingQueue(request);
}
}