Skip to content

Commit 410aa76

Browse files
committed
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.
1 parent 38bace4 commit 410aa76

2 files changed

Lines changed: 48 additions & 56 deletions

File tree

README.md

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

TESTING.md

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -145,44 +145,34 @@ 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
174-
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.
152+
2. Start the emulator:
153+
```shell
154+
$ gcloud beta emulators pubsub start
155+
```
177156

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-
```
157+
3. Point your client to the emulator.
158+
The code below assumes that you have set the `host` and `port`
159+
to the host and port the emulator is on.
160+
```java
161+
ChannelProvider channelProvider =
162+
// SubscriptionAdminSettings works too.
163+
TopicAdminSettings.defaultChannelProviderBuilder()
164+
.setServiceAddress(host)
165+
.setPort(port)
166+
.setCredentialsProvider(
167+
FixedCredentialsProvider.create(NoCredentials.getInstance()))
168+
.build();
169+
TopicAdminClient topicClient = TopicAdminClient.create(
170+
TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build());
171+
SubscriptionAdminClient subscriptionClient = SubscriptionAdminClient.create(
172+
SubscriptionAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build());
173+
Publisher publisher =
174+
Publisher.newBuilder(topicName).setChannelProvider(channelProvider).build();
175+
```
186176

187177
### Testing code that uses Resource Manager
188178

0 commit comments

Comments
 (0)