Skip to content

Commit c42d5d3

Browse files
authored
pubsub: remove obsolete doc references (#1823)
This PR removes references to the deprecated code of the docs. The emulator section is rewritten. Fixes #1789.
1 parent e5b3b78 commit c42d5d3

File tree

2 files changed

+49
-55
lines changed

2 files changed

+49
-55
lines changed

README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -586,29 +586,31 @@ Google Cloud Pub/Sub (Alpha)
586586
Here is a code snippet showing a simple usage example from within Compute Engine/App Engine
587587
Flexible. Note that you must [supply credentials](#authentication) and a project ID if running this
588588
snippet elsewhere. Complete source code can be found at
589-
[CreateSubscriptionAndPullMessages.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateSubscriptionAndPullMessages.java).
589+
[CreateTopicAndPublishMessages.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java).
590590
591591
```java
592-
import com.google.cloud.pubsub.Message;
593-
import com.google.cloud.pubsub.PubSub;
594-
import com.google.cloud.pubsub.PubSub.MessageConsumer;
595-
import com.google.cloud.pubsub.PubSub.MessageProcessor;
596-
import com.google.cloud.pubsub.PubSubOptions;
597-
import com.google.cloud.pubsub.Subscription;
598-
import com.google.cloud.pubsub.SubscriptionInfo;
599-
600-
try (PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) {
601-
Subscription subscription =
602-
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
603-
MessageProcessor callback = new MessageProcessor() {
604-
@Override
605-
public void process(Message message) throws Exception {
606-
System.out.printf("Received message \"%s\"%n", message.getPayloadAsString());
607-
}
608-
};
609-
// Create a message consumer and pull messages (for 60 seconds)
610-
try (MessageConsumer consumer = subscription.pullAsync(callback)) {
611-
Thread.sleep(60_000);
592+
import com.google.api.gax.core.ApiFuture;
593+
import com.google.cloud.pubsub.spi.v1.Publisher;
594+
import com.google.cloud.pubsub.spi.v1.TopicAdminClient;
595+
import com.google.protobuf.ByteString;
596+
import com.google.pubsub.v1.PubsubMessage;
597+
import com.google.pubsub.v1.TopicName;
598+
599+
TopicName topic = TopicName.create("test-project", "test-topic");
600+
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
601+
topicAdminClient.createTopic(topic);
602+
}
603+
604+
Publisher publisher = null;
605+
try {
606+
publisher = Publisher.newBuilder(topic).build();
607+
ByteString data = ByteString.copyFromUtf8("my message");
608+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
609+
ApiFuture<String> messageId = publisher.publish(pubsubMessage);
610+
System.out.println("published with message ID: " + messageId.get());
611+
} finally {
612+
if (publisher != null) {
613+
publisher.shutdown();
612614
}
613615
}
614616
```

TESTING.md

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -145,44 +145,36 @@ uses the `RemoteLoggingHelper` to create a metric.
145145

146146
### Testing code that uses Pub/Sub
147147

148-
#### On your machine
149-
150-
You can test against a temporary local Pub/Sub by following these steps:
151-
152-
1. Start the local Pub/Sub emulator before running your tests using `LocalPubSubHelper`'s `create`
153-
and `start` methods. This will bind a port for communication with the local Pub/Sub emulator.
154-
```java
155-
LocalPubSubHelper helper = LocalPubSubHelper.create();
156-
157-
helper.start(); // Starts the local Pub/Sub emulator in a separate process
158-
```
148+
You can test against a Pub/Sub emulator:
159149

160-
2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For
161-
example:
162-
```java
163-
PubSub localPubsub = helper.getOptions().getService();
164-
```
165-
166-
3. Run your tests.
150+
1. [Install Cloud SDK](https://cloud.google.com/sdk/downloads)
167151

168-
4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so:
169-
```java
170-
helper.stop();
171-
```
172-
173-
#### On a remote machine
152+
2. Start the emulator:
153+
```shell
154+
$ gcloud beta emulators pubsub start
155+
```
174156

175-
You can test against a remote Pub/Sub emulator as well. To do this, set the `PubSubOptions` project
176-
endpoint to the hostname of the remote machine, like the example below.
157+
To determine which host/port the emulator is running on:
158+
```shell
159+
$ gcloud beta emulators pubsub env-init
160+
# Sample output:
161+
# export PUBSUB_EMULATOR_HOST=localhost:8759
162+
```
177163

178-
```java
179-
PubSubOptions options = PubSubOptions.newBuilder()
180-
.setProjectId("my-project-id") // must match project ID specified on remote machine
181-
.setHost("<hostname of machine>:<port>")
182-
.setCredentials(NoCredentials.getInstance())
183-
.build();
184-
PubSub localPubsub = options.getService();
185-
```
164+
3. Point your client to the emulator.
165+
```java
166+
ChannelProvider channelProvider =
167+
// SubscriptionAdminSettings works too.
168+
TopicAdminSettings.defaultChannelProviderBuilder()
169+
.setEndpoint(System.getenv("PUBSUB_EMULATOR_HOST"))
170+
.setCredentialsProvider(
171+
FixedCredentialsProvider.create(NoCredentials.getInstance()))
172+
.build();
173+
TopicAdminClient topicClient = TopicAdminClient.create(
174+
TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build());
175+
Publisher publisher =
176+
Publisher.newBuilder(topicName).setChannelProvider(channelProvider).build();
177+
```
186178

187179
### Testing code that uses Resource Manager
188180

0 commit comments

Comments
 (0)