Skip to content

Commit

Permalink
samples: subscription detachment (#289)
Browse files Browse the repository at this point in the history
* samples: subscription detachment

* nit: println

* remove dlq policy
  • Loading branch information
anguillanneuf authored Sep 1, 2020
1 parent 0d97689 commit 785981a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 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_detach_subscription]
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.pubsub.v1.DetachSubscriptionRequest;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.Subscription;
import java.io.IOException;

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

detachSubscriptionExample(projectId, subscriptionId);
}

public static void detachSubscriptionExample(String projectId, String subscriptionId)
throws IOException {
ProjectSubscriptionName subscriptionName =
ProjectSubscriptionName.of(projectId, subscriptionId);

try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
topicAdminClient.detachSubscription(
DetachSubscriptionRequest.newBuilder()
.setSubscription(subscriptionName.toString())
.build());
}

try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
Subscription subscription = subscriptionAdminClient.getSubscription(subscriptionName);
if (subscription.getDetached()) {
System.out.println("Subscription is detached.");
} else {
System.out.println("Subscription is NOT detached.");
}
}
}
}
// [END pubsub_detach_subscription]
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public static void removeDeadLetterPolicyExample(
ProjectSubscriptionName.of(projectId, subscriptionId);
TopicName topicName = TopicName.of(projectId, topicId);

System.out.println(
"Before: " + subscriptionAdminClient.getSubscription(subscriptionName).getAllFields());

// Construct the subscription you expect to have after the request. Here,
// values in the required fields (name, topic) help identify the subscription.
// No dead letter policy is supplied.
Expand All @@ -61,9 +58,7 @@ public static void removeDeadLetterPolicyExample(
// Construct a field mask to indicate which field to update in the subscription.
FieldMask updateMask =
FieldMask.newBuilder()
.addPaths("dead_letter_policy.dead_letter_topic")
// A default of 5 is applied upon successful update.
.addPaths("dead_letter_policy.max_delivery_attempts")
.addPaths("dead_letter_policy")
.build();

UpdateSubscriptionRequest request =
Expand Down
6 changes: 6 additions & 0 deletions samples/snippets/src/test/java/pubsub/AdminIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,19 @@ public void testAdmin() throws Exception {
assertThat(bout.toString()).contains("permissions: \"pubsub.subscriptions.consume\"");
assertThat(bout.toString()).contains("permissions: \"pubsub.subscriptions.update\"");

bout.reset();
// Test subscription detachment.
DetachSubscriptionExample.detachSubscriptionExample(projectId, pullSubscriptionId);
assertThat(bout.toString()).contains("Subscription is detached.");

bout.reset();
// Test create a subscription with ordering
CreateSubscriptionWithOrdering.createSubscriptionWithOrderingExample(
projectId, topicId, orderedSubscriptionId);
assertThat(bout.toString()).contains("Created a subscription with ordering");
assertThat(bout.toString()).contains("enable_message_ordering=true");


bout.reset();
// Test delete subscription. Run twice to delete both pull and push subscriptions.
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, pullSubscriptionId);
Expand Down
3 changes: 1 addition & 2 deletions samples/snippets/src/test/java/pubsub/DeadLetterQueueIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public void testQuickstart() throws Exception {
bout.reset();
// Remove dead letter policy.
RemoveDeadLetterPolicyExample.removeDeadLetterPolicyExample(projectId, subscriptionId, topicId);
assertThat(bout.toString())
.contains("google.pubsub.v1.Subscription.dead_letter_policy=max_delivery_attempts: 5");
assertThat(bout.toString()).doesNotContain("dead_letter_policy");
}
}

0 comments on commit 785981a

Please sign in to comment.