Skip to content

Commit

Permalink
samples: add subscription with filter example (#978)
Browse files Browse the repository at this point in the history
* samples: add subscription with filter example

* 🦉 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* populate filter string

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
anguillanneuf and gcf-owl-bot[bot] committed Jan 20, 2022
1 parent 76a7200 commit 4654a58
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-pubsub/tree/m
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| Create Topic With Schema Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateTopicWithSchemaExample.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/CreateTopicWithSchemaExample.java) |
Expand Down
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_create_subscription_with_filter]
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.ProjectTopicName;
import com.google.pubsub.v1.Subscription;
import java.io.IOException;

public class CreateSubscriptionWithFiltering {
public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String topicId = "your-topic-id";
String subscriptionId = "your-subscription-id";
String filter = "attributes.author=\"unknown\"";

createSubscriptionWithFilteringExample(projectId, topicId, subscriptionId, filter);
}

public static void createSubscriptionWithFilteringExample(
String projectId, String topicId, String subscriptionId, String filter) throws IOException {
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {

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

Subscription subscription =
subscriptionAdminClient.createSubscription(
Subscription.newBuilder()
.setName(subscriptionName.toString())
.setTopic(topicName.toString())
// Receive messages with attribute key "author" and value "unknown".
.setFilter(filter)
.build());

System.out.println(
"Created a subscription with filtering enabled: " + subscription.getAllFields());
}
}
}
// [END pubsub_create_subscription_with_filter]
12 changes: 12 additions & 0 deletions samples/snippets/src/test/java/pubsub/AdminIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class AdminIT {
private static final String pullSubscriptionId = "iam-pull-subscription-" + _suffix;
private static final String pushSubscriptionId = "iam-push-subscription-" + _suffix;
private static final String orderedSubscriptionId = "iam-ordered-subscription-" + _suffix;
private static final String filteredSubscriptionId = "iam-filtered-subscription-" + _suffix;
private static final String pushEndpoint = "https://my-test-project.appspot.com/push";

private static final TopicName topicName = TopicName.of(projectId, topicId);
Expand All @@ -53,6 +54,8 @@ public class AdminIT {
SubscriptionName.of(projectId, pushSubscriptionId);
private static final SubscriptionName orderedSubscriptionName =
SubscriptionName.of(projectId, orderedSubscriptionId);
private static final SubscriptionName filteredSubscriptionName =
SubscriptionName.of(projectId, filteredSubscriptionId);

private static void requireEnvVar(String varName) {
assertNotNull(
Expand Down Expand Up @@ -82,6 +85,7 @@ public void tearDown() throws Exception {
subscriptionAdminClient.deleteSubscription(pullSubscriptionName);
subscriptionAdminClient.deleteSubscription(pushSubscriptionName);
subscriptionAdminClient.deleteSubscription(orderedSubscriptionName);
subscriptionAdminClient.deleteSubscription(filteredSubscriptionName);
} catch (NotFoundException ignored) {
// ignore this as resources may not have been created
}
Expand Down Expand Up @@ -183,6 +187,14 @@ public void testAdmin() throws Exception {
assertThat(bout.toString()).contains("Created a subscription with ordering");
assertThat(bout.toString()).contains("enable_message_ordering=true");

bout.reset();
// Test create a subscription with filtering enabled
CreateSubscriptionWithFiltering.createSubscriptionWithFilteringExample(
projectId, topicId, filteredSubscriptionId, "attributes.author=\"unknown\"");
assertThat(bout.toString()).contains("Created a subscription with filtering enabled");
assertThat(bout.toString())
.contains("google.pubsub.v1.Subscription.filter=attributes.author=\"unknown\"");

bout.reset();
// Test delete subscription. Run twice to delete both pull and push subscriptions.
DeleteSubscriptionExample.deleteSubscriptionExample(projectId, pullSubscriptionId);
Expand Down
4 changes: 0 additions & 4 deletions samples/snippets/src/test/java/pubsub/SubscriberIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminClient;
Expand Down Expand Up @@ -58,8 +56,6 @@ public class SubscriberIT {
private static final TopicName topicName = TopicName.of(projectId, topicId);
private static final ProjectSubscriptionName subscriptionName =
ProjectSubscriptionName.of(projectId, subscriptionId);
private static final ExecutorProvider executorProvider =
InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(4).build();

private static void requireEnvVar(String varName) {
assertNotNull(
Expand Down

0 comments on commit 4654a58

Please sign in to comment.