/** * 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()) { * ProjectSubscriptionName subscription = ProjectSubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]"); * List<String> ackIds = new ArrayList<>(); * subscriptionAdminClient.acknowledge(subscription.toString(), 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.rpc.ApiException if the remote call fails */ /* package-private */ final void acknowledge(String subscription, List<String> ackIds) { AcknowledgeRequest request = AcknowledgeRequest.newBuilder().setSubscription(subscription).addAllAckIds(ackIds).build(); acknowledge(request); }
.addAllAckIds(ackIds) .build(); acknowledge(request);
@Test @SuppressWarnings("all") public void acknowledgeExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockSubscriber.addException(exception); try { ProjectSubscriptionName subscription = ProjectSubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]"); List<String> ackIds = new ArrayList<>(); client.acknowledge(subscription, ackIds); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void acknowledgeTest() { Empty expectedResponse = Empty.newBuilder().build(); mockSubscriber.addResponse(expectedResponse); ProjectSubscriptionName subscription = ProjectSubscriptionName.of("[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, ProjectSubscriptionName.parse(actualRequest.getSubscription())); Assert.assertEquals(ackIds, actualRequest.getAckIdsList()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }