Skip to content

Commit 0376d2c

Browse files
samples: create subscription with exactly once delivery enabled (#1032)
* 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>
1 parent 8edcf79 commit 0376d2c

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

java-pubsub/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-pubsub/tree/m
245245
| 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) |
246246
| 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) |
247247
| 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) |
248+
| 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) |
248249
| 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) |
249250
| 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) |
250251
| 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) |
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pubsub;
18+
19+
// [START pubsub_create_subscription_with_exactly_once_delivery]
20+
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
21+
import com.google.pubsub.v1.ProjectSubscriptionName;
22+
import com.google.pubsub.v1.ProjectTopicName;
23+
import com.google.pubsub.v1.Subscription;
24+
import java.io.IOException;
25+
26+
public class CreateSubscriptionWithExactlyOnceDelivery {
27+
public static void main(String... args) throws Exception {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "your-project-id";
30+
String topicId = "your-topic-id";
31+
String subscriptionId = "your-subscription-id";
32+
33+
createSubscriptionWithExactlyOnceDeliveryExample(projectId, topicId, subscriptionId);
34+
}
35+
36+
public static void createSubscriptionWithExactlyOnceDeliveryExample(
37+
String projectId, String topicId, String subscriptionId) throws IOException {
38+
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
39+
40+
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
41+
ProjectSubscriptionName subscriptionName =
42+
ProjectSubscriptionName.of(projectId, subscriptionId);
43+
44+
Subscription subscription =
45+
subscriptionAdminClient.createSubscription(
46+
Subscription.newBuilder()
47+
.setName(subscriptionName.toString())
48+
.setTopic(topicName.toString())
49+
// Enable exactly once delivery in the subscription.
50+
.setEnableExactlyOnceDelivery(true)
51+
.build());
52+
53+
System.out.println(
54+
"Created a subscription with exactly once delivery enabled: "
55+
+ subscription.getAllFields());
56+
}
57+
}
58+
}
59+
// [END pubsub_create_subscription_with_exactly_once_delivery]

java-pubsub/samples/snippets/src/test/java/pubsub/AdminIT.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public class AdminIT {
4545
private static final String pushSubscriptionId = "iam-push-subscription-" + _suffix;
4646
private static final String orderedSubscriptionId = "iam-ordered-subscription-" + _suffix;
4747
private static final String filteredSubscriptionId = "iam-filtered-subscription-" + _suffix;
48+
private static final String exactlyOnceSubscriptionId =
49+
"iam-exactly-once-subscription-" + _suffix;
4850
private static final String pushEndpoint = "https://my-test-project.appspot.com/push";
4951

5052
private static final TopicName topicName = TopicName.of(projectId, topicId);
@@ -56,6 +58,8 @@ public class AdminIT {
5658
SubscriptionName.of(projectId, orderedSubscriptionId);
5759
private static final SubscriptionName filteredSubscriptionName =
5860
SubscriptionName.of(projectId, filteredSubscriptionId);
61+
private static final SubscriptionName exactlyOnceSubscriptionName =
62+
SubscriptionName.of(projectId, exactlyOnceSubscriptionId);
5963

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

198203
bout.reset();
199-
// Test delete subscription. Run twice to delete both pull and push subscriptions.
204+
// Test create a subscription with exactly once delivery enabled
205+
CreateSubscriptionWithExactlyOnceDelivery.createSubscriptionWithExactlyOnceDeliveryExample(
206+
projectId, topicId, exactlyOnceSubscriptionId);
207+
assertThat(bout.toString())
208+
.contains("Created a subscription with exactly once delivery enabled:");
209+
assertThat(bout.toString()).contains("enable_exactly_once_delivery=true");
210+
211+
bout.reset();
212+
// Test delete subscription.
200213
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, pullSubscriptionId);
201214
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, pushSubscriptionId);
202215
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, orderedSubscriptionId);
216+
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, exactlyOnceSubscriptionId);
203217
assertThat(bout.toString()).contains("Deleted subscription.");
204218

205219
bout.reset();

0 commit comments

Comments
 (0)