Skip to content

Commit 663da74

Browse files
committed
pubsub: correct emulator sample
Previously we use TopicAdminSettings.defaultBuilder(). The default channel uses secure connection, which is incompatible with the emulator. This commit corrects this. We cannot yet provide a way for users to set plain text in InstantiatingChannelProvider, since usePlaintext is still a beta API.
1 parent 95f0dd7 commit 663da74

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

TESTING.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,30 @@ $ gcloud beta emulators pubsub env-init
147147

148148
3. Point your client to the emulator.
149149
```java
150-
ChannelProvider channelProvider =
151-
// SubscriptionAdminSettings works too.
152-
TopicAdminSettings.defaultChannelProviderBuilder()
153-
.setEndpoint(System.getenv("PUBSUB_EMULATOR_HOST"))
154-
.setCredentialsProvider(
155-
FixedCredentialsProvider.create(NoCredentials.getInstance()))
156-
.build();
157-
TopicAdminClient topicClient = TopicAdminClient.create(
158-
TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build());
159-
Publisher publisher =
160-
Publisher.newBuilder(topicName).setChannelProvider(channelProvider).build();
150+
String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
151+
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext(true).build();
152+
try {
153+
ChannelProvider channelProvider = FixedChannelProvider.create(channel);
154+
CredentialsProvider credentialsProvider = new NoCredentialsProvider();
155+
156+
// Similarly for SubscriptionAdminSettings
157+
TopicAdminClient topicClient = TopicAdminClient.create(
158+
TopicAdminSettings
159+
.defaultBuilder()
160+
.setChannelProvider(channelProvider)
161+
.setCredentialsProvider(credentialsProvider)
162+
.build());
163+
164+
// Similarly for Subscriber
165+
Publisher publisher =
166+
Publisher
167+
.newBuilder(topicName)
168+
.setChannelProvider(channelProvider)
169+
.setCredentialsProvider(credentialsProvider)
170+
.build();
171+
} finally {
172+
channel.shutdown();
173+
}
161174
```
162175

163176
### Testing code that uses Resource Manager

0 commit comments

Comments
 (0)