Skip to content

Commit 21d5cfc

Browse files
authored
docs: Update emulator sample to create a topic and publish to it (#2039)
* deps: Change scope of grpc-inprocess dependency from runtime to test * docs: Update emulator sample to create a topic and publish to it * docs: Small wording update to emulator sample
1 parent f890585 commit 21d5cfc

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
package pubsub;
1818

19+
/**
20+
* Snippet that demonstrates creating Pub/Sub clients using the Google Cloud Pub/Sub emulator.
21+
*
22+
* <p>Note: clients cannot start/stop the emulator.
23+
*/
24+
25+
// [START pubsub_use_emulator]
26+
27+
import com.google.api.core.ApiFuture;
1928
import com.google.api.gax.core.CredentialsProvider;
2029
import com.google.api.gax.core.NoCredentialsProvider;
2130
import com.google.api.gax.grpc.GrpcTransportChannel;
@@ -24,20 +33,16 @@
2433
import com.google.cloud.pubsub.v1.Publisher;
2534
import com.google.cloud.pubsub.v1.TopicAdminClient;
2635
import com.google.cloud.pubsub.v1.TopicAdminSettings;
36+
import com.google.protobuf.ByteString;
37+
import com.google.pubsub.v1.PubsubMessage;
38+
import com.google.pubsub.v1.Topic;
2739
import com.google.pubsub.v1.TopicName;
2840
import io.grpc.ManagedChannel;
2941
import io.grpc.ManagedChannelBuilder;
3042
import java.io.IOException;
3143

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-
*/
3744
public class UsePubSubEmulatorExample {
38-
39-
public static void main(String... args) throws IOException {
40-
// [START pubsub_use_emulator]
45+
public static void main(String... args) throws Exception {
4146
String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
4247
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
4348
try {
@@ -46,25 +51,36 @@ public static void main(String... args) throws IOException {
4651
CredentialsProvider credentialsProvider = NoCredentialsProvider.create();
4752

4853
// Set the channel and credentials provider when creating a `TopicAdminClient`.
49-
// Similarly for SubscriptionAdminClient
50-
TopicAdminClient topicClient =
54+
// Can be done similarly for a `SubscriptionAdminClient`.
55+
TopicAdminClient topicAdminClient =
5156
TopicAdminClient.create(
5257
TopicAdminSettings.newBuilder()
5358
.setTransportChannelProvider(channelProvider)
5459
.setCredentialsProvider(credentialsProvider)
5560
.build());
5661

5762
TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
63+
Topic topic = topicAdminClient.createTopic(topicName);
64+
System.out.println("Created topic: " + topic.getName());
65+
5866
// Set the channel and credentials provider when creating a `Publisher`.
59-
// Similarly for Subscriber
67+
// Can be done similarly for a `Subscriber`.
6068
Publisher publisher =
6169
Publisher.newBuilder(topicName)
6270
.setChannelProvider(channelProvider)
6371
.setCredentialsProvider(credentialsProvider)
6472
.build();
73+
74+
String message = "Hello World!";
75+
ByteString data = ByteString.copyFromUtf8(message);
76+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
77+
78+
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
79+
String messageId = messageIdFuture.get();
80+
System.out.println("Published message ID: " + messageId);
6581
} finally {
6682
channel.shutdown();
6783
}
68-
// [END pubsub_use_emulator]
6984
}
7085
}
86+
// [END pubsub_use_emulator]

0 commit comments

Comments
 (0)