From 410aa7607dfb7259c1ef48d3698bd8c2c643e743 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Wed, 29 Mar 2017 11:54:39 +1100 Subject: [PATCH 1/3] pubsub: remove obsolete doc references This PR removes references to the deprecated code of the docs. The emulator section is rewritten. Example for Subscriber is missing because it still uses ChannelBuilder. We plan to migrate it to ChannelProvider soon. Updates #1789. --- README.md | 44 ++++++++++++++++++++------------------- TESTING.md | 60 +++++++++++++++++++++++------------------------------- 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index daf50b29979d..3a09008203d5 100644 --- a/README.md +++ b/README.md @@ -585,29 +585,31 @@ Google Cloud Pub/Sub (Alpha) Here is a code snippet showing a simple usage example from within Compute Engine/App Engine Flexible. Note that you must [supply credentials](#authentication) and a project ID if running this snippet elsewhere. Complete source code can be found at -[CreateSubscriptionAndPullMessages.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateSubscriptionAndPullMessages.java). +[CreateTopicAndPublishMessages.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java). ```java -import com.google.cloud.pubsub.Message; -import com.google.cloud.pubsub.PubSub; -import com.google.cloud.pubsub.PubSub.MessageConsumer; -import com.google.cloud.pubsub.PubSub.MessageProcessor; -import com.google.cloud.pubsub.PubSubOptions; -import com.google.cloud.pubsub.Subscription; -import com.google.cloud.pubsub.SubscriptionInfo; - -try (PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) { - Subscription subscription = - pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription")); - MessageProcessor callback = new MessageProcessor() { - @Override - public void process(Message message) throws Exception { - System.out.printf("Received message \"%s\"%n", message.getPayloadAsString()); - } - }; - // Create a message consumer and pull messages (for 60 seconds) - try (MessageConsumer consumer = subscription.pullAsync(callback)) { - Thread.sleep(60_000); +import com.google.api.gax.core.ApiFuture; +import com.google.cloud.pubsub.spi.v1.Publisher; +import com.google.cloud.pubsub.spi.v1.TopicAdminClient; +import com.google.protobuf.ByteString; +import com.google.pubsub.v1.PubsubMessage; +import com.google.pubsub.v1.TopicName; + +TopicName topic = TopicName.create("test-project", "test-topic"); +try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.createTopic(topic); +} + +Publisher publisher = null; +try { + publisher = Publisher.newBuilder(topic).build(); + ByteString data = ByteString.copyFromUtf8("my message"); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + ApiFuture messageId = publisher.publish(pubsubMessage); + System.out.println("published with message ID: " + messageId.get()); +} finally { + if (publisher != null) { + publisher.shutdown(); } } ``` diff --git a/TESTING.md b/TESTING.md index b97f23918268..4e8d40a82781 100644 --- a/TESTING.md +++ b/TESTING.md @@ -145,44 +145,34 @@ uses the `RemoteLoggingHelper` to create a metric. ### Testing code that uses Pub/Sub -#### On your machine - -You can test against a temporary local Pub/Sub by following these steps: - -1. Start the local Pub/Sub emulator before running your tests using `LocalPubSubHelper`'s `create` -and `start` methods. This will bind a port for communication with the local Pub/Sub emulator. - ```java - LocalPubSubHelper helper = LocalPubSubHelper.create(); - - helper.start(); // Starts the local Pub/Sub emulator in a separate process - ``` +You can test against a Pub/Sub emulator: -2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For -example: - ```java - PubSub localPubsub = helper.getOptions().getService(); - ``` - -3. Run your tests. +1. [Install Cloud SDK](https://cloud.google.com/sdk/downloads) -4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so: - ```java - helper.stop(); - ``` - -#### On a remote machine - -You can test against a remote Pub/Sub emulator as well. To do this, set the `PubSubOptions` project -endpoint to the hostname of the remote machine, like the example below. +2. Start the emulator: +```shell +$ gcloud beta emulators pubsub start +``` - ```java - PubSubOptions options = PubSubOptions.newBuilder() - .setProjectId("my-project-id") // must match project ID specified on remote machine - .setHost(":") - .setCredentials(NoCredentials.getInstance()) - .build(); - PubSub localPubsub = options.getService(); - ``` +3. Point your client to the emulator. +The code below assumes that you have set the `host` and `port` +to the host and port the emulator is on. +```java +ChannelProvider channelProvider = + // SubscriptionAdminSettings works too. + TopicAdminSettings.defaultChannelProviderBuilder() + .setServiceAddress(host) + .setPort(port) + .setCredentialsProvider( + FixedCredentialsProvider.create(NoCredentials.getInstance())) + .build(); +TopicAdminClient topicClient = TopicAdminClient.create( + TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build()); +SubscriptionAdminClient subscriptionClient = SubscriptionAdminClient.create( + SubscriptionAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build()); +Publisher publisher = + Publisher.newBuilder(topicName).setChannelProvider(channelProvider).build(); +``` ### Testing code that uses Resource Manager From bdf1034090ced11cb9baf71f3c071511c7bb9d15 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Thu, 30 Mar 2017 13:49:52 +1100 Subject: [PATCH 2/3] pr comment --- TESTING.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/TESTING.md b/TESTING.md index 4e8d40a82781..23c106a7b33d 100644 --- a/TESTING.md +++ b/TESTING.md @@ -154,6 +154,13 @@ You can test against a Pub/Sub emulator: $ gcloud beta emulators pubsub start ``` +To determine which host/port the emulator is running on: +```shell +$ gcloud beta emulators pubsub env-init +# Sample output: +# export PUBSUB_EMULATOR_HOST=localhost:8759 +``` + 3. Point your client to the emulator. The code below assumes that you have set the `host` and `port` to the host and port the emulator is on. @@ -168,8 +175,6 @@ ChannelProvider channelProvider = .build(); TopicAdminClient topicClient = TopicAdminClient.create( TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build()); -SubscriptionAdminClient subscriptionClient = SubscriptionAdminClient.create( - SubscriptionAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build()); Publisher publisher = Publisher.newBuilder(topicName).setChannelProvider(channelProvider).build(); ``` From c37ba09d8d5a5a3f15dbe272592f422806b31ead Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Mon, 3 Apr 2017 16:36:34 +1000 Subject: [PATCH 3/3] pr comment --- TESTING.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/TESTING.md b/TESTING.md index 23c106a7b33d..779545202ab3 100644 --- a/TESTING.md +++ b/TESTING.md @@ -162,14 +162,11 @@ $ gcloud beta emulators pubsub env-init ``` 3. Point your client to the emulator. -The code below assumes that you have set the `host` and `port` -to the host and port the emulator is on. ```java ChannelProvider channelProvider = // SubscriptionAdminSettings works too. TopicAdminSettings.defaultChannelProviderBuilder() - .setServiceAddress(host) - .setPort(port) + .setEndpoint(System.getenv("PUBSUB_EMULATOR_HOST")) .setCredentialsProvider( FixedCredentialsProvider.create(NoCredentials.getInstance())) .build();