Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, like it's predecessor looks wrong - shouldn't this be a constant defined in the library?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivery attempt is not a constant defined in the library. In fact, it is best-effort only, and when no dead letter policy is attached, users won't be able to access delivery attempts.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood - if it's understood as something important and understood as something special - it should be defined in the library in some way.

.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");
}
}