Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 1dc651c

Browse files
samples: add DLQ samples (#197)
1 parent 55bdc26 commit 1dc651c

9 files changed

Lines changed: 541 additions & 0 deletions

File tree

samples/install-without-bom/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
~ Copyright 2020 Google LLC
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
217
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
318
<modelVersion>4.0.0</modelVersion>
419
<groupId>com.google.cloud</groupId>

samples/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
~ Copyright 2020 Google LLC
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
217
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
318
<modelVersion>4.0.0</modelVersion>
419
<groupId>com.google.cloud</groupId>

samples/snapshot/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
~ Copyright 2020 Google LLC
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
217
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
318
<modelVersion>4.0.0</modelVersion>
419
<groupId>com.google.cloud</groupId>

samples/snippets/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
~ Copyright 2020 Google LLC
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
217
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
318
<modelVersion>4.0.0</modelVersion>
419
<groupId>com.google.cloud</groupId>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2020 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_dead_letter_create_subscription]
20+
21+
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
22+
import com.google.pubsub.v1.DeadLetterPolicy;
23+
import com.google.pubsub.v1.ProjectSubscriptionName;
24+
import com.google.pubsub.v1.ProjectTopicName;
25+
import com.google.pubsub.v1.Subscription;
26+
27+
public class CreateSubscriptionWithDeadLetterPolicyExample {
28+
29+
public static void main(String... args) throws Exception {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "Your Project ID";
32+
// This is the subscription you want to create with a dead letter policy.
33+
String subscriptionId = "Your Subscription ID";
34+
// This is an existing topic that you want to attach the subscription with dead letter policy
35+
// to.
36+
String topicId = "Your Topic ID";
37+
// This is an existing topic that the subscription with dead letter policy forwards dead letter
38+
// messages to.
39+
String deadLetterTopicId = "Your Dead Letter Topic ID";
40+
41+
CreateSubscriptionWithDeadLetterPolicyExample.createSubscriptionWithDeadLetterPolicyExample(
42+
projectId, subscriptionId, topicId, deadLetterTopicId);
43+
}
44+
45+
public static void createSubscriptionWithDeadLetterPolicyExample(
46+
String projectId, String subscriptionId, String topicId, String deadLetterTopicId)
47+
throws Exception {
48+
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
49+
50+
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
51+
ProjectSubscriptionName subscriptionName =
52+
ProjectSubscriptionName.of(projectId, subscriptionId);
53+
ProjectTopicName deadLetterTopicName = ProjectTopicName.of(projectId, deadLetterTopicId);
54+
55+
DeadLetterPolicy deadLetterPolicy =
56+
DeadLetterPolicy.newBuilder()
57+
.setDeadLetterTopic(deadLetterTopicName.toString())
58+
// The maximum number of times that the service attempts to deliver a
59+
// message before forwarding it to the dead letter topic. Must be [5-100].
60+
.setMaxDeliveryAttempts(10)
61+
.build();
62+
63+
Subscription request =
64+
Subscription.newBuilder()
65+
.setName(subscriptionName.toString())
66+
.setTopic(topicName.toString())
67+
.setDeadLetterPolicy(deadLetterPolicy)
68+
.build();
69+
70+
Subscription subscription = subscriptionAdminClient.createSubscription(request);
71+
72+
System.out.println("Created subscription: " + subscription.getName());
73+
System.out.println(
74+
"It will forward dead letter messages to: "
75+
+ subscription.getDeadLetterPolicy().getDeadLetterTopic());
76+
System.out.println(
77+
"After "
78+
+ subscription.getDeadLetterPolicy().getMaxDeliveryAttempts()
79+
+ " delivery attempts.");
80+
// Remember to attach a subscription to the dead letter topic because
81+
// messages published to a topic with no subscriptions are lost.
82+
}
83+
}
84+
}
85+
// [END pubsub_dead_letter_create_subscription]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2020 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_dead_letter_delivery_attempt]
20+
21+
import com.google.cloud.pubsub.v1.AckReplyConsumer;
22+
import com.google.cloud.pubsub.v1.MessageReceiver;
23+
import com.google.cloud.pubsub.v1.Subscriber;
24+
import com.google.pubsub.v1.ProjectSubscriptionName;
25+
import com.google.pubsub.v1.PubsubMessage;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class ReceiveMessagesWithDeliveryAttemptsExample {
30+
31+
public static void main(String... args) throws Exception {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "Your Project ID";
34+
// This is an existing subscription with a dead letter policy.
35+
String subscriptionId = "Your Subscription ID";
36+
37+
ReceiveMessagesWithDeliveryAttemptsExample.receiveMessagesWithDeliveryAttemptsExample(
38+
projectId, subscriptionId);
39+
}
40+
41+
public static void receiveMessagesWithDeliveryAttemptsExample(
42+
String projectId, String subscriptionId) {
43+
44+
ProjectSubscriptionName subscriptionName =
45+
ProjectSubscriptionName.of(projectId, subscriptionId);
46+
47+
// Instantiate an asynchronous message receiver.
48+
MessageReceiver receiver =
49+
new MessageReceiver() {
50+
@Override
51+
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
52+
// Handle incoming message, then ack the received message.
53+
System.out.println("Id: " + message.getMessageId());
54+
System.out.println("Data: " + message.getData().toStringUtf8());
55+
System.out.println("Delivery Attempt: " + Subscriber.getDeliveryAttempt(message));
56+
consumer.ack();
57+
}
58+
};
59+
60+
Subscriber subscriber = null;
61+
try {
62+
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
63+
// Start the subscriber.
64+
subscriber.startAsync().awaitRunning();
65+
System.out.printf("Listening for messages on %s:\n", subscriptionName.toString());
66+
// Allow the subscriber to run for 30s unless an unrecoverable error occurs.
67+
subscriber.awaitTerminated(30, TimeUnit.SECONDS);
68+
} catch (TimeoutException timeoutException) {
69+
// Shut down the subscriber after 30s. Stop receiving messages.
70+
subscriber.stopAsync();
71+
}
72+
}
73+
}
74+
// [END pubsub_dead_letter_delivery_attempt]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2020 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_dead_letter_remove]
20+
21+
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
22+
import com.google.protobuf.FieldMask;
23+
import com.google.pubsub.v1.ProjectSubscriptionName;
24+
import com.google.pubsub.v1.Subscription;
25+
import com.google.pubsub.v1.TopicName;
26+
import com.google.pubsub.v1.UpdateSubscriptionRequest;
27+
28+
public class RemoveDeadLetterPolicyExample {
29+
30+
public static void main(String... args) throws Exception {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "Your Project ID";
33+
// This is an existing subscription with dead letter policy.
34+
String subscriptionId = "Your Subscription ID";
35+
// This is an existing topic that the subscription with dead letter policy is attached to.
36+
String topicId = "Your Topic ID";
37+
38+
RemoveDeadLetterPolicyExample.removeDeadLetterPolicyExample(projectId, subscriptionId, topicId);
39+
}
40+
41+
public static void removeDeadLetterPolicyExample(
42+
String projectId, String subscriptionId, String topicId) throws Exception {
43+
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
44+
45+
ProjectSubscriptionName subscriptionName =
46+
ProjectSubscriptionName.of(projectId, subscriptionId);
47+
TopicName topicName = TopicName.of(projectId, topicId);
48+
49+
System.out.println(
50+
"Before: " + subscriptionAdminClient.getSubscription(subscriptionName).getAllFields());
51+
52+
// Construct the subscription you expect to have after the request. Here,
53+
// values in the required fields (name, topic) help identify the subscription.
54+
// No dead letter policy is supplied.
55+
Subscription expectedSubscription =
56+
Subscription.newBuilder()
57+
.setName(subscriptionName.toString())
58+
.setTopic(topicName.toString())
59+
.build();
60+
61+
// Construct a field mask to indicate which field to update in the subscription.
62+
FieldMask updateMask =
63+
FieldMask.newBuilder()
64+
.addPaths("dead_letter_policy.dead_letter_topic")
65+
// A default of 5 is applied upon successful update.
66+
.addPaths("dead_letter_policy.max_delivery_attempts")
67+
.build();
68+
69+
UpdateSubscriptionRequest request =
70+
UpdateSubscriptionRequest.newBuilder()
71+
.setSubscription(expectedSubscription)
72+
.setUpdateMask(updateMask)
73+
.build();
74+
75+
Subscription response = subscriptionAdminClient.updateSubscription(request);
76+
77+
// You should see an empty dead letter topic field inside the dead letter policy.
78+
System.out.println("After: " + response.getAllFields());
79+
}
80+
}
81+
}
82+
// [END pubsub_dead_letter_remove]

0 commit comments

Comments
 (0)