Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: add DLQ samples #197

Merged
merged 11 commits into from May 20, 2020
15 changes: 15 additions & 0 deletions samples/install-without-bom/pom.xml
@@ -1,4 +1,19 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
~ 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.
-->
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
Expand Down
15 changes: 15 additions & 0 deletions samples/pom.xml
@@ -1,4 +1,19 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
~ 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.
-->
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
Expand Down
15 changes: 15 additions & 0 deletions samples/snapshot/pom.xml
@@ -1,4 +1,19 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
~ 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.
-->
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
Expand Down
15 changes: 15 additions & 0 deletions samples/snippets/pom.xml
@@ -1,4 +1,19 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
~ 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.
-->
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
Expand Down
@@ -0,0 +1,85 @@
/*
* 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_dead_letter_create_subscription]

import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.DeadLetterPolicy;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.ProjectTopicName;
import com.google.pubsub.v1.Subscription;

public class CreateSubscriptionWithDeadLetterPolicyExample {

public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "Your Project ID";
// This is the subscription you want to create with a dead letter policy.
String subscriptionId = "Your Subscription ID";
// This is an existing topic that you want to attach the subscription with dead letter policy
// to.
String topicId = "Your Topic ID";
// This is an existing topic that the subscription with dead letter policy forwards dead letter
// messages to.
String deadLetterTopicId = "Your Dead Letter Topic ID";

CreateSubscriptionWithDeadLetterPolicyExample.createSubscriptionWithDeadLetterPolicyExample(
projectId, subscriptionId, topicId, deadLetterTopicId);
}

public static void createSubscriptionWithDeadLetterPolicyExample(
String projectId, String subscriptionId, String topicId, String deadLetterTopicId)
throws Exception {
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {

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

DeadLetterPolicy deadLetterPolicy =
DeadLetterPolicy.newBuilder()
.setDeadLetterTopic(deadLetterTopicName.toString())
// The maximum number of times that the service attempts to deliver a
// message before forwarding it to the dead letter topic. Must be [5-100].
.setMaxDeliveryAttempts(10)
.build();

Subscription request =
Subscription.newBuilder()
.setName(subscriptionName.toString())
.setTopic(topicName.toString())
.setDeadLetterPolicy(deadLetterPolicy)
.build();

Subscription subscription = subscriptionAdminClient.createSubscription(request);

System.out.println("Created subscription: " + subscription.getName());
System.out.println(
"It will forward dead letter messages to: "
+ subscription.getDeadLetterPolicy().getDeadLetterTopic());
System.out.println(
"After "
+ subscription.getDeadLetterPolicy().getMaxDeliveryAttempts()
+ " delivery attempts.");
// Remember to attach a subscription to the dead letter topic because
// messages published to a topic with no subscriptions are lost.
}
}
}
// [END pubsub_dead_letter_create_subscription]
@@ -0,0 +1,74 @@
/*
* 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_dead_letter_delivery_attempt]

import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ReceiveMessagesWithDeliveryAttemptsExample {

public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "Your Project ID";
// This is an existing subscription with a dead letter policy.
String subscriptionId = "Your Subscription ID";

ReceiveMessagesWithDeliveryAttemptsExample.receiveMessagesWithDeliveryAttemptsExample(
projectId, subscriptionId);
}

public static void receiveMessagesWithDeliveryAttemptsExample(
String projectId, String subscriptionId) {

ProjectSubscriptionName subscriptionName =
ProjectSubscriptionName.of(projectId, subscriptionId);

// Instantiate an asynchronous message receiver.
MessageReceiver receiver =
new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
// Handle incoming message, then ack the received message.
System.out.println("Id: " + message.getMessageId());
System.out.println("Data: " + message.getData().toStringUtf8());
System.out.println("Delivery Attempt: " + Subscriber.getDeliveryAttempt(message));
consumer.ack();
}
};

Subscriber subscriber = null;
try {
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
// Start the subscriber.
subscriber.startAsync().awaitRunning();
System.out.printf("Listening for messages on %s:\n", subscriptionName.toString());
// Allow the subscriber to run for 30s unless an unrecoverable error occurs.
subscriber.awaitTerminated(30, TimeUnit.SECONDS);
} catch (TimeoutException timeoutException) {
// Shut down the subscriber after 30s. Stop receiving messages.
subscriber.stopAsync();
}
}
}
// [END pubsub_dead_letter_delivery_attempt]
@@ -0,0 +1,82 @@
/*
* 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_dead_letter_remove]

import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.protobuf.FieldMask;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.Subscription;
import com.google.pubsub.v1.TopicName;
import com.google.pubsub.v1.UpdateSubscriptionRequest;

public class RemoveDeadLetterPolicyExample {

public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "Your Project ID";
// This is an existing subscription with dead letter policy.
String subscriptionId = "Your Subscription ID";
// This is an existing topic that the subscription with dead letter policy is attached to.
String topicId = "Your Topic ID";

RemoveDeadLetterPolicyExample.removeDeadLetterPolicyExample(projectId, subscriptionId, topicId);
}

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

ProjectSubscriptionName subscriptionName =
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.
Subscription expectedSubscription =
Subscription.newBuilder()
.setName(subscriptionName.toString())
.setTopic(topicName.toString())
.build();

// 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")
.build();

UpdateSubscriptionRequest request =
UpdateSubscriptionRequest.newBuilder()
.setSubscription(expectedSubscription)
.setUpdateMask(updateMask)
.build();

Subscription response = subscriptionAdminClient.updateSubscription(request);

// You should see an empty dead letter topic field inside the dead letter policy.
System.out.println("After: " + response.getAllFields());
}
}
}
// [END pubsub_dead_letter_remove]