1616
1717package 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 ;
1928import com .google .api .gax .core .CredentialsProvider ;
2029import com .google .api .gax .core .NoCredentialsProvider ;
2130import com .google .api .gax .grpc .GrpcTransportChannel ;
2433import com .google .cloud .pubsub .v1 .Publisher ;
2534import com .google .cloud .pubsub .v1 .TopicAdminClient ;
2635import 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 ;
2739import com .google .pubsub .v1 .TopicName ;
2840import io .grpc .ManagedChannel ;
2941import io .grpc .ManagedChannelBuilder ;
3042import 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- */
3744public 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