Skip to content

Commit 8ded110

Browse files
docs: adding pubsub emulator example (#1602)
* adding pubsub emulator example * docs: adding pubsub emulator example * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 2c5ed5e commit 8ded110

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-pubsub/tree/m
304304
| Test Topic Permissions Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/TestTopicPermissionsExample.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/TestTopicPermissionsExample.java) |
305305
| Update Dead Letter Policy Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/UpdateDeadLetterPolicyExample.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/UpdateDeadLetterPolicyExample.java) |
306306
| Update Push Configuration Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/UpdatePushConfigurationExample.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/UpdatePushConfigurationExample.java) |
307+
| Use Pub Sub Emulator Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.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/UsePubSubEmulatorExample.java) |
307308
| Update Topic Schema Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/UpdateTopicSchemaExample.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/UpdateTopicSchemaExample.java) |
308309
| State | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/utilities/State.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/utilities/State.java) |
309310
| State Proto | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/utilities/StateProto.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/utilities/StateProto.java) |
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017 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+
import com.google.api.gax.core.CredentialsProvider;
20+
import com.google.api.gax.core.NoCredentialsProvider;
21+
import com.google.api.gax.grpc.GrpcTransportChannel;
22+
import com.google.api.gax.rpc.FixedTransportChannelProvider;
23+
import com.google.api.gax.rpc.TransportChannelProvider;
24+
import com.google.cloud.pubsub.v1.Publisher;
25+
import com.google.cloud.pubsub.v1.TopicAdminClient;
26+
import com.google.cloud.pubsub.v1.TopicAdminSettings;
27+
import com.google.pubsub.v1.TopicName;
28+
import io.grpc.ManagedChannel;
29+
import io.grpc.ManagedChannelBuilder;
30+
import java.io.IOException;
31+
32+
/**
33+
* Snippet that demonstrates creating Pub/Sub clients using the Google Cloud Pub/Sub emulator.
34+
*
35+
* <p>Note: clients cannot start/stop the emulator.
36+
*/
37+
public class UsePubSubEmulatorExample {
38+
39+
public static void main(String... args) throws IOException {
40+
// [START pubsub_use_emulator]
41+
String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
42+
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
43+
try {
44+
TransportChannelProvider channelProvider =
45+
FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
46+
CredentialsProvider credentialsProvider = NoCredentialsProvider.create();
47+
48+
// Set the channel and credentials provider when creating a `TopicAdminClient`.
49+
// Similarly for SubscriptionAdminClient
50+
TopicAdminClient topicClient =
51+
TopicAdminClient.create(
52+
TopicAdminSettings.newBuilder()
53+
.setTransportChannelProvider(channelProvider)
54+
.setCredentialsProvider(credentialsProvider)
55+
.build());
56+
57+
TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
58+
// Set the channel and credentials provider when creating a `Publisher`.
59+
// Similarly for Subscriber
60+
Publisher publisher =
61+
Publisher.newBuilder(topicName)
62+
.setChannelProvider(channelProvider)
63+
.setCredentialsProvider(credentialsProvider)
64+
.build();
65+
} finally {
66+
channel.shutdown();
67+
}
68+
// [END pubsub_use_emulator]
69+
}
70+
}

0 commit comments

Comments
 (0)