use of com.google.cloud.pubsub.spi.v1.MessageDispatcher.OutstandingMessagesBatch.OutstandingMessage in project google-cloud-java by GoogleCloudPlatform.
the class MessageDispatcher method processOutstandingBatches.
public void processOutstandingBatches() {
while (true) {
boolean batchDone = false;
Runnable batchCallback = null;
OutstandingMessage outstandingMessage;
synchronized (outstandingMessageBatches) {
OutstandingMessagesBatch nextBatch = outstandingMessageBatches.peek();
if (nextBatch == null) {
return;
}
outstandingMessage = nextBatch.messages.peek();
if (outstandingMessage == null) {
return;
}
try {
// This is a non-blocking flow controller.
flowController.reserve(1, outstandingMessage.receivedMessage().getMessage().getSerializedSize());
} catch (FlowController.MaxOutstandingElementCountReachedException | FlowController.MaxOutstandingRequestBytesReachedException flowControlException) {
return;
} catch (FlowControlException unexpectedException) {
throw new IllegalStateException("Flow control unexpected exception", unexpectedException);
}
// We got a hold to the message already.
nextBatch.messages.poll();
batchDone = nextBatch.messages.isEmpty();
if (batchDone) {
outstandingMessageBatches.poll();
batchCallback = nextBatch.doneCallback;
}
}
final PubsubMessage message = outstandingMessage.receivedMessage().getMessage();
final AckHandler ackHandler = outstandingMessage.ackHandler();
final SettableFuture<AckReply> response = SettableFuture.create();
final AckReplyConsumer consumer = new AckReplyConsumer() {
@Override
public void ack() {
response.set(AckReply.ACK);
}
@Override
public void nack() {
response.set(AckReply.NACK);
}
};
Futures.addCallback(response, ackHandler);
executor.submit(new Runnable() {
@Override
public void run() {
try {
receiver.receiveMessage(message, consumer);
} catch (Exception e) {
response.setException(e);
}
}
});
if (batchDone) {
batchCallback.run();
}
}
}
Aggregations