Search in sources :

Example 1 with AcknowledgeRequest

use of com.google.pubsub.v1.AcknowledgeRequest in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientTest method acknowledgeTest.

@Test
@SuppressWarnings("all")
public void acknowledgeTest() {
    Empty expectedResponse = Empty.newBuilder().build();
    mockSubscriber.addResponse(expectedResponse);
    SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
    List<String> ackIds = new ArrayList<>();
    client.acknowledge(subscription, ackIds);
    List<GeneratedMessageV3> actualRequests = mockSubscriber.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AcknowledgeRequest actualRequest = (AcknowledgeRequest) actualRequests.get(0);
    Assert.assertEquals(subscription, actualRequest.getSubscriptionAsSubscriptionName());
    Assert.assertEquals(ackIds, actualRequest.getAckIdsList());
}
Also used : Empty(com.google.protobuf.Empty) AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest) ArrayList(java.util.ArrayList) SubscriptionName(com.google.pubsub.v1.SubscriptionName) ByteString(com.google.protobuf.ByteString) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 2 with AcknowledgeRequest

use of com.google.pubsub.v1.AcknowledgeRequest in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplate method pull.

/**
 * Pulls messages synchronously, on demand, using the pull request in argument.
 *
 * <p>This method acknowledges all received messages.
 * @param pullRequest pull request containing the subscription name
 * @return the list of {@link PubsubMessage} containing the headers and payload
 */
private List<PubsubMessage> pull(PullRequest pullRequest, RetrySettings retrySettings) {
    Assert.notNull(pullRequest, "The pull request cannot be null.");
    try {
        SubscriberStub subscriber = this.subscriberFactory.createSubscriberStub(retrySettings);
        Assert.notNull(subscriber, "A SubscriberStub is needed to execute the pull request.");
        PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);
        // Ack received messages.
        if (pullResponse.getReceivedMessagesCount() > 0) {
            List<String> ackIds = pullResponse.getReceivedMessagesList().stream().map(ReceivedMessage::getAckId).collect(Collectors.toList());
            AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder().setSubscription(pullRequest.getSubscription()).addAllAckIds(ackIds).build();
            subscriber.acknowledgeCallable().call(acknowledgeRequest);
        }
        return pullResponse.getReceivedMessagesList().stream().map(ReceivedMessage::getMessage).collect(Collectors.toList());
    } catch (Exception ioe) {
        throw new PubSubException("Error pulling messages from subscription " + pullRequest.getSubscription() + ".", ioe);
    }
}
Also used : PullResponse(com.google.pubsub.v1.PullResponse) AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest) SubscriberStub(com.google.cloud.pubsub.v1.stub.SubscriberStub) ByteString(com.google.protobuf.ByteString)

Example 3 with AcknowledgeRequest

use of com.google.pubsub.v1.AcknowledgeRequest in project flink by apache.

the class BlockingGrpcPubSubSubscriber method acknowledge.

@Override
public void acknowledge(List<String> acknowledgementIds) {
    if (acknowledgementIds.isEmpty()) {
        return;
    }
    // grpc servers won't accept acknowledge requests that are too large so we split the ackIds
    Tuple2<List<String>, List<String>> splittedAckIds = splitAckIds(acknowledgementIds);
    while (!splittedAckIds.f0.isEmpty()) {
        AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder().setSubscription(projectSubscriptionName).addAllAckIds(splittedAckIds.f0).build();
        stub.withDeadlineAfter(60, SECONDS).acknowledge(acknowledgeRequest);
        splittedAckIds = splitAckIds(splittedAckIds.f1);
    }
}
Also used : AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Example 4 with AcknowledgeRequest

use of com.google.pubsub.v1.AcknowledgeRequest in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClient method acknowledge.

// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
   * Acknowledges the messages associated with the `ack_ids` in the `AcknowledgeRequest`. The
   * Pub/Sub system can remove the relevant messages from the subscription.
   *
   * <p>Acknowledging a message whose ack deadline has expired may succeed, but such a message may
   * be redelivered later. Acknowledging a message more than once will not result in an error.
   *
   * <p>Sample code:
   *
   * <pre><code>
   * try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
   *   SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
   *   List&lt;String&gt; ackIds = new ArrayList&lt;&gt;();
   *   subscriptionAdminClient.acknowledge(subscription, ackIds);
   * }
   * </code></pre>
   *
   * @param subscription The subscription whose message is being acknowledged. Format is
   *     `projects/{project}/subscriptions/{sub}`.
   * @param ackIds The acknowledgment ID for the messages being acknowledged that was returned by
   *     the Pub/Sub system in the `Pull` response. Must not be empty.
   * @throws com.google.api.gax.grpc.ApiException if the remote call fails
   */
/* package-private */
final void acknowledge(SubscriptionName subscription, List<String> ackIds) {
    AcknowledgeRequest request = AcknowledgeRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).addAllAckIds(ackIds).build();
    acknowledge(request);
}
Also used : AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest)

Example 5 with AcknowledgeRequest

use of com.google.pubsub.v1.AcknowledgeRequest in project beam by apache.

the class PubsubGrpcClient method acknowledge.

@Override
public void acknowledge(SubscriptionPath subscription, List<String> ackIds) throws IOException {
    AcknowledgeRequest request = AcknowledgeRequest.newBuilder().setSubscription(subscription.getPath()).addAllAckIds(ackIds).build();
    // ignore Empty result.
    subscriberStub().acknowledge(request);
}
Also used : AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest)

Aggregations

AcknowledgeRequest (com.google.pubsub.v1.AcknowledgeRequest)6 ByteString (com.google.protobuf.ByteString)2 SubscriberStub (com.google.cloud.pubsub.v1.stub.SubscriberStub)1 Empty (com.google.protobuf.Empty)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 PullResponse (com.google.pubsub.v1.PullResponse)1 SubscriptionName (com.google.pubsub.v1.SubscriptionName)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Test (org.junit.Test)1