From cf754914eb7b9cfa3a2551de24cc5d914e96e924 Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Mon, 20 May 2024 15:26:09 +0000 Subject: [PATCH 1/3] deps: Change scope of grpc-inprocess dependency from runtime to test --- google-cloud-pubsub/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-pubsub/pom.xml b/google-cloud-pubsub/pom.xml index cbbe43bba..271e12728 100644 --- a/google-cloud-pubsub/pom.xml +++ b/google-cloud-pubsub/pom.xml @@ -85,7 +85,7 @@ io.grpc grpc-inprocess - runtime + test com.google.errorprone From b3572b3b2eac7e9e551d418c5278d014389b6c2a Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Mon, 20 May 2024 16:38:19 +0000 Subject: [PATCH 2/3] docs: Update emulator sample to create a topic and publish to it --- .../java/pubsub/UsePubSubEmulatorExample.java | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java b/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java index f4c69eac4..e5cecf6ca 100644 --- a/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java +++ b/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java @@ -16,6 +16,15 @@ package pubsub; +/** + * Snippet that demonstrates creating Pub/Sub clients using the Google Cloud Pub/Sub emulator. + * + *

Note: clients cannot start/stop the emulator. + */ + +// [START pubsub_use_emulator] + +import com.google.api.core.ApiFuture; import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.grpc.GrpcTransportChannel; @@ -24,20 +33,16 @@ import com.google.cloud.pubsub.v1.Publisher; import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.pubsub.v1.TopicAdminSettings; +import com.google.protobuf.ByteString; +import com.google.pubsub.v1.PubsubMessage; +import com.google.pubsub.v1.Topic; import com.google.pubsub.v1.TopicName; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import java.io.IOException; -/** - * Snippet that demonstrates creating Pub/Sub clients using the Google Cloud Pub/Sub emulator. - * - *

Note: clients cannot start/stop the emulator. - */ public class UsePubSubEmulatorExample { - - public static void main(String... args) throws IOException { - // [START pubsub_use_emulator] + public static void main(String... args) throws Exception { String hostport = System.getenv("PUBSUB_EMULATOR_HOST"); ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build(); try { @@ -46,8 +51,8 @@ public static void main(String... args) throws IOException { CredentialsProvider credentialsProvider = NoCredentialsProvider.create(); // Set the channel and credentials provider when creating a `TopicAdminClient`. - // Similarly for SubscriptionAdminClient - TopicAdminClient topicClient = + // Similarly for a `SubscriptionAdminClient`. + TopicAdminClient topicAdminClient = TopicAdminClient.create( TopicAdminSettings.newBuilder() .setTransportChannelProvider(channelProvider) @@ -55,16 +60,27 @@ public static void main(String... args) throws IOException { .build()); TopicName topicName = TopicName.of("my-project-id", "my-topic-id"); + Topic topic = topicAdminClient.createTopic(topicName); + System.out.println("Created topic: " + topic.getName()); + // Set the channel and credentials provider when creating a `Publisher`. - // Similarly for Subscriber + // Similarly for a `Subscriber`. Publisher publisher = Publisher.newBuilder(topicName) .setChannelProvider(channelProvider) .setCredentialsProvider(credentialsProvider) .build(); + + String message = "Hello World!"; + ByteString data = ByteString.copyFromUtf8(message); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + + ApiFuture messageIdFuture = publisher.publish(pubsubMessage); + String messageId = messageIdFuture.get(); + System.out.println("Published message ID: " + messageId); } finally { channel.shutdown(); } - // [END pubsub_use_emulator] } } +// [END pubsub_use_emulator] \ No newline at end of file From 8cf7b6ca9f0ff618a7c75c427f9afdfa7c2d989c Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Thu, 3 Oct 2024 16:52:51 +0000 Subject: [PATCH 3/3] docs: Small wording update to emulator sample --- .../src/main/java/pubsub/UsePubSubEmulatorExample.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java b/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java index e5cecf6ca..5da0bd7f8 100644 --- a/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java +++ b/samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java @@ -51,7 +51,7 @@ public static void main(String... args) throws Exception { CredentialsProvider credentialsProvider = NoCredentialsProvider.create(); // Set the channel and credentials provider when creating a `TopicAdminClient`. - // Similarly for a `SubscriptionAdminClient`. + // Can be done similarly for a `SubscriptionAdminClient`. TopicAdminClient topicAdminClient = TopicAdminClient.create( TopicAdminSettings.newBuilder() @@ -64,7 +64,7 @@ public static void main(String... args) throws Exception { System.out.println("Created topic: " + topic.getName()); // Set the channel and credentials provider when creating a `Publisher`. - // Similarly for a `Subscriber`. + // Can be done similarly for a `Subscriber`. Publisher publisher = Publisher.newBuilder(topicName) .setChannelProvider(channelProvider)