use of com.google.pubsub.v1.PublishRequest 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);
}
});
}
use of com.google.pubsub.v1.PublishRequest in project google-cloud-java by GoogleCloudPlatform.
the class FakePublisherServiceImpl method publish.
@Override
public void publish(PublishRequest request, StreamObserver<PublishResponse> responseObserver) {
requests.add(request);
Response response;
try {
response = publishResponses.take();
} catch (InterruptedException e) {
throw new IllegalArgumentException(e);
}
if (response.isError()) {
responseObserver.onError(response.getError());
} else {
responseObserver.onNext(response.getPublishResponse());
responseObserver.onCompleted();
}
}
use of com.google.pubsub.v1.PublishRequest in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientTest method publishTest.
@Test
@SuppressWarnings("all")
public void publishTest() {
String messageIdsElement = "messageIdsElement-744837059";
List<String> messageIds = Arrays.asList(messageIdsElement);
PublishResponse expectedResponse = PublishResponse.newBuilder().addAllMessageIds(messageIds).build();
mockPublisher.addResponse(expectedResponse);
TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
ByteString data = ByteString.copyFromUtf8("-86");
PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build();
List<PubsubMessage> messages = Arrays.asList(messagesElement);
PublishResponse actualResponse = client.publish(topic, messages);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests();
Assert.assertEquals(1, actualRequests.size());
PublishRequest actualRequest = (PublishRequest) actualRequests.get(0);
Assert.assertEquals(topic, actualRequest.getTopicAsTopicName());
Assert.assertEquals(messages, actualRequest.getMessagesList());
}
use of com.google.pubsub.v1.PublishRequest in project beam by apache.
the class PubsubGrpcClientTest method publishOneMessage.
@Test
public void publishOneMessage() throws IOException {
String expectedTopic = TOPIC.getPath();
PubsubMessage expectedPubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom(DATA.getBytes())).putAllAttributes(ATTRIBUTES).putAllAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID)).build();
final PublishRequest expectedRequest = PublishRequest.newBuilder().setTopic(expectedTopic).addAllMessages(ImmutableList.of(expectedPubsubMessage)).build();
final PublishResponse response = PublishResponse.newBuilder().addAllMessageIds(ImmutableList.of(MESSAGE_ID)).build();
final List<PublishRequest> requestsReceived = new ArrayList<>();
PublisherImplBase publisherImplBase = new PublisherImplBase() {
@Override
public void publish(PublishRequest request, StreamObserver<PublishResponse> responseObserver) {
requestsReceived.add(request);
responseObserver.onNext(response);
responseObserver.onCompleted();
}
};
Server server = InProcessServerBuilder.forName(channelName).addService(publisherImplBase).build().start();
try {
OutgoingMessage actualMessage = new OutgoingMessage(DATA.getBytes(), ATTRIBUTES, MESSAGE_TIME, RECORD_ID);
int n = client.publish(TOPIC, ImmutableList.of(actualMessage));
assertEquals(1, n);
assertEquals(expectedRequest, Iterables.getOnlyElement(requestsReceived));
} finally {
server.shutdownNow();
}
}
Aggregations