Search in sources :

Example 1 with FlowControlException

use of com.google.api.gax.batching.FlowController.FlowControlException 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();
        }
    }
}
Also used : OutstandingMessage(com.google.cloud.pubsub.spi.v1.MessageDispatcher.OutstandingMessagesBatch.OutstandingMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) FlowControlException(com.google.api.gax.batching.FlowController.FlowControlException) FlowControlException(com.google.api.gax.batching.FlowController.FlowControlException)

Aggregations

FlowControlException (com.google.api.gax.batching.FlowController.FlowControlException)1 OutstandingMessage (com.google.cloud.pubsub.spi.v1.MessageDispatcher.OutstandingMessagesBatch.OutstandingMessage)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1