use of com.google.pubsub.v1.PublisherGrpc.PublisherFutureStub in project google-cloud-java by GoogleCloudPlatform.
the class Publisher method publishOutstandingBatch.
private void publishOutstandingBatch(final OutstandingBatch outstandingBatch) {
PublishRequest.Builder publishRequest = PublishRequest.newBuilder();
publishRequest.setTopic(cachedTopicNameString);
for (OutstandingPublish outstandingPublish : outstandingBatch.outstandingPublishes) {
publishRequest.addMessages(outstandingPublish.message);
}
int currentChannel = channelIndex.next();
long rpcTimeoutMs = Math.round(retrySettings.getInitialRpcTimeout().toMillis() * Math.pow(retrySettings.getRpcTimeoutMultiplier(), outstandingBatch.attempt - 1));
rpcTimeoutMs = Math.min(rpcTimeoutMs, retrySettings.getMaxRpcTimeout().toMillis());
PublisherFutureStub stub = PublisherGrpc.newFutureStub(channels[currentChannel]).withDeadlineAfter(rpcTimeoutMs, TimeUnit.MILLISECONDS);
if (callCredentials != null) {
stub = stub.withCallCredentials(callCredentials);
}
Futures.addCallback(stub.publish(publishRequest.build()), new FutureCallback<PublishResponse>() {
@Override
public void onSuccess(PublishResponse result) {
try {
if (result.getMessageIdsCount() != outstandingBatch.size()) {
Throwable t = new IllegalStateException(String.format("The publish result count %s does not match " + "the expected %s results. Please contact Cloud Pub/Sub support " + "if this frequently occurs", result.getMessageIdsCount(), outstandingBatch.size()));
for (OutstandingPublish oustandingMessage : outstandingBatch.outstandingPublishes) {
oustandingMessage.publishResult.setException(t);
}
return;
}
Iterator<OutstandingPublish> messagesResultsIt = outstandingBatch.outstandingPublishes.iterator();
for (String messageId : result.getMessageIdsList()) {
messagesResultsIt.next().publishResult.set(messageId);
}
} finally {
flowController.release(outstandingBatch.size(), outstandingBatch.batchSizeBytes);
messagesWaiter.incrementPendingMessages(-outstandingBatch.size());
}
}
@Override
public void onFailure(Throwable t) {
long nextBackoffDelay = computeNextBackoffDelayMs(outstandingBatch, retrySettings, longRandom);
if (!isRetryable(t) || retrySettings.getMaxAttempts() > 0 && outstandingBatch.getAttempt() > retrySettings.getMaxAttempts() || System.currentTimeMillis() + nextBackoffDelay > outstandingBatch.creationTime + retrySettings.getTotalTimeout().toMillis()) {
try {
for (OutstandingPublish outstandingPublish : outstandingBatch.outstandingPublishes) {
outstandingPublish.publishResult.setException(t);
}
} finally {
messagesWaiter.incrementPendingMessages(-outstandingBatch.size());
}
return;
}
executor.schedule(new Runnable() {
@Override
public void run() {
publishOutstandingBatch(outstandingBatch);
}
}, nextBackoffDelay, TimeUnit.MILLISECONDS);
}
});
}
Aggregations