Search in sources :

Example 1 with PublishRequest

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);
        }
    });
}
Also used : PublishResponse(com.google.pubsub.v1.PublishResponse) PublishRequest(com.google.pubsub.v1.PublishRequest) PublisherFutureStub(com.google.pubsub.v1.PublisherGrpc.PublisherFutureStub) Iterator(java.util.Iterator)

Example 2 with PublishRequest

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();
    }
}
Also used : PublishResponse(com.google.pubsub.v1.PublishResponse)

Example 3 with PublishRequest

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());
}
Also used : PublishResponse(com.google.pubsub.v1.PublishResponse) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) PublishRequest(com.google.pubsub.v1.PublishRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 4 with PublishRequest

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();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) PublishResponse(com.google.pubsub.v1.PublishResponse) OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) Server(io.grpc.Server) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) PublishRequest(com.google.pubsub.v1.PublishRequest) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PublisherImplBase(com.google.pubsub.v1.PublisherGrpc.PublisherImplBase) Test(org.junit.Test)

Aggregations

PublishResponse (com.google.pubsub.v1.PublishResponse)4 PublishRequest (com.google.pubsub.v1.PublishRequest)3 ByteString (com.google.protobuf.ByteString)2 PubsubMessage (com.google.pubsub.v1.PubsubMessage)2 Test (org.junit.Test)2 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 PublisherFutureStub (com.google.pubsub.v1.PublisherGrpc.PublisherFutureStub)1 PublisherImplBase (com.google.pubsub.v1.PublisherGrpc.PublisherImplBase)1 TopicName (com.google.pubsub.v1.TopicName)1 Server (io.grpc.Server)1 StreamObserver (io.grpc.stub.StreamObserver)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 OutgoingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage)1