Skip to content

Commit

Permalink
samples: create subscription with exactly once delivery enabled (#1032)
Browse files Browse the repository at this point in the history
* samples: create subscription with exactly once delivery enabled

* lint

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
anguillanneuf and gcf-owl-bot[bot] committed Mar 4, 2022
1 parent 81c9cc1 commit 7e08361
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -245,6 +245,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-pubsub/tree/m
| Create Pull Subscription Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreatePullSubscriptionExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreatePullSubscriptionExample.java) |
| Create Push Subscription Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreatePushSubscriptionExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreatePushSubscriptionExample.java) |
| Create Subscription With Dead Letter Policy Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateSubscriptionWithDeadLetterPolicyExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreateSubscriptionWithDeadLetterPolicyExample.java) |
| Create Subscription With Exactly Once Delivery | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateSubscriptionWithExactlyOnceDelivery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreateSubscriptionWithExactlyOnceDelivery.java) |
| Create Subscription With Filtering | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateSubscriptionWithFiltering.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreateSubscriptionWithFiltering.java) |
| Create Subscription With Ordering | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateSubscriptionWithOrdering.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreateSubscriptionWithOrdering.java) |
| Create Topic Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateTopicExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreateTopicExample.java) |
Expand Down
@@ -0,0 +1,59 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pubsub;

// [START pubsub_create_subscription_with_exactly_once_delivery]
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.ProjectTopicName;
import com.google.pubsub.v1.Subscription;
import java.io.IOException;

public class CreateSubscriptionWithExactlyOnceDelivery {
public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String topicId = "your-topic-id";
String subscriptionId = "your-subscription-id";

createSubscriptionWithExactlyOnceDeliveryExample(projectId, topicId, subscriptionId);
}

public static void createSubscriptionWithExactlyOnceDeliveryExample(
String projectId, String topicId, String subscriptionId) throws IOException {
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {

ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
ProjectSubscriptionName subscriptionName =
ProjectSubscriptionName.of(projectId, subscriptionId);

Subscription subscription =
subscriptionAdminClient.createSubscription(
Subscription.newBuilder()
.setName(subscriptionName.toString())
.setTopic(topicName.toString())
// Enable exactly once delivery in the subscription.
.setEnableExactlyOnceDelivery(true)
.build());

System.out.println(
"Created a subscription with exactly once delivery enabled: "
+ subscription.getAllFields());
}
}
}
// [END pubsub_create_subscription_with_exactly_once_delivery]
16 changes: 15 additions & 1 deletion samples/snippets/src/test/java/pubsub/AdminIT.java
Expand Up @@ -45,6 +45,8 @@ public class AdminIT {
private static final String pushSubscriptionId = "iam-push-subscription-" + _suffix;
private static final String orderedSubscriptionId = "iam-ordered-subscription-" + _suffix;
private static final String filteredSubscriptionId = "iam-filtered-subscription-" + _suffix;
private static final String exactlyOnceSubscriptionId =
"iam-exactly-once-subscription-" + _suffix;
private static final String pushEndpoint = "https://my-test-project.appspot.com/push";

private static final TopicName topicName = TopicName.of(projectId, topicId);
Expand All @@ -56,6 +58,8 @@ public class AdminIT {
SubscriptionName.of(projectId, orderedSubscriptionId);
private static final SubscriptionName filteredSubscriptionName =
SubscriptionName.of(projectId, filteredSubscriptionId);
private static final SubscriptionName exactlyOnceSubscriptionName =
SubscriptionName.of(projectId, exactlyOnceSubscriptionId);

private static void requireEnvVar(String varName) {
assertNotNull(
Expand Down Expand Up @@ -86,6 +90,7 @@ public void tearDown() throws Exception {
subscriptionAdminClient.deleteSubscription(pushSubscriptionName);
subscriptionAdminClient.deleteSubscription(orderedSubscriptionName);
subscriptionAdminClient.deleteSubscription(filteredSubscriptionName);
subscriptionAdminClient.deleteSubscription(exactlyOnceSubscriptionName);
} catch (NotFoundException ignored) {
// ignore this as resources may not have been created
}
Expand Down Expand Up @@ -196,10 +201,19 @@ public void testAdmin() throws Exception {
.contains("google.pubsub.v1.Subscription.filter=attributes.author=\"unknown\"");

bout.reset();
// Test delete subscription. Run twice to delete both pull and push subscriptions.
// Test create a subscription with exactly once delivery enabled
CreateSubscriptionWithExactlyOnceDelivery.createSubscriptionWithExactlyOnceDeliveryExample(
projectId, topicId, exactlyOnceSubscriptionId);
assertThat(bout.toString())
.contains("Created a subscription with exactly once delivery enabled:");
assertThat(bout.toString()).contains("enable_exactly_once_delivery=true");

bout.reset();
// Test delete subscription.
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, pullSubscriptionId);
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, pushSubscriptionId);
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, orderedSubscriptionId);
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, exactlyOnceSubscriptionId);
assertThat(bout.toString()).contains("Deleted subscription.");

bout.reset();
Expand Down

0 comments on commit 7e08361

Please sign in to comment.