private void flush() {
// TODO - need to do a better job of routing messages when there are multiple endpoints. This is just going to forward all queued messages to the first
// waiting endpoint, even if there are multiple waiting to do work
EndPointQueue selected = waiting.isEmpty() ? null : waiting.get(0);
while (!queue.isEmpty()) {
InterHubMessage message = queue.get(0);
switch (message.getDelivery()) {
case Stateful:
case AllHandlers:
if (endpoints.isEmpty()) {
return;
}
if (message.getDelivery() == InterHubMessage.Delivery.Stateful) {
initializer.onStatefulMessage(message);
}
for (EndPointQueue endpoint : endpoints) {
endpoint.dispatch(message);
}
queue.remove(0);
waiting.clear();
continue;
case SingleHandler:
if (selected == null) {
return;
}
queue.remove(0);
waiting.remove(selected);
selected.dispatch(message);
break;
default:
throw new IllegalArgumentException("Unknown delivery type: " + message.getDelivery());
}
}
}