Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Update emulator sample to create a topic and publish to it #2039

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google-cloud-pubsub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-inprocess</artifactId>
<scope>runtime</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
Expand Down
40 changes: 28 additions & 12 deletions samples/snippets/src/main/java/pubsub/UsePubSubEmulatorExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@

package pubsub;

/**
* Snippet that demonstrates creating Pub/Sub clients using the Google Cloud Pub/Sub emulator.
*
* <p>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;
Expand All @@ -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.
*
* <p>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 {
Expand All @@ -46,25 +51,36 @@ 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 =
// Can be done similarly for a `SubscriptionAdminClient`.
TopicAdminClient topicAdminClient =
TopicAdminClient.create(
TopicAdminSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.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
// Can be done 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<String> 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]
Loading