16
16
17
17
package pubsub ;
18
18
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 ;
19
28
import com .google .api .gax .core .CredentialsProvider ;
20
29
import com .google .api .gax .core .NoCredentialsProvider ;
21
30
import com .google .api .gax .grpc .GrpcTransportChannel ;
24
33
import com .google .cloud .pubsub .v1 .Publisher ;
25
34
import com .google .cloud .pubsub .v1 .TopicAdminClient ;
26
35
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 ;
27
39
import com .google .pubsub .v1 .TopicName ;
28
40
import io .grpc .ManagedChannel ;
29
41
import io .grpc .ManagedChannelBuilder ;
30
42
import java .io .IOException ;
31
43
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
44
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 {
41
46
String hostport = System .getenv ("PUBSUB_EMULATOR_HOST" );
42
47
ManagedChannel channel = ManagedChannelBuilder .forTarget (hostport ).usePlaintext ().build ();
43
48
try {
@@ -46,25 +51,36 @@ public static void main(String... args) throws IOException {
46
51
CredentialsProvider credentialsProvider = NoCredentialsProvider .create ();
47
52
48
53
// 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 =
51
56
TopicAdminClient .create (
52
57
TopicAdminSettings .newBuilder ()
53
58
.setTransportChannelProvider (channelProvider )
54
59
.setCredentialsProvider (credentialsProvider )
55
60
.build ());
56
61
57
62
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
+
58
66
// Set the channel and credentials provider when creating a `Publisher`.
59
- // Similarly for Subscriber
67
+ // Can be done similarly for a ` Subscriber`.
60
68
Publisher publisher =
61
69
Publisher .newBuilder (topicName )
62
70
.setChannelProvider (channelProvider )
63
71
.setCredentialsProvider (credentialsProvider )
64
72
.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 );
65
81
} finally {
66
82
channel .shutdown ();
67
83
}
68
- // [END pubsub_use_emulator]
69
84
}
70
85
}
86
+ // [END pubsub_use_emulator]
0 commit comments