Skip to content
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
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> messageId = publisher.publish(pubsubMessage);
System.out.println("published with message ID: " + messageId.get());
} finally {
if (publisher != null) {
publisher.shutdown();
}
}
```
Expand Down
60 changes: 26 additions & 34 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,44 +145,36 @@ 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
2. Start the emulator:
```shell
$ gcloud beta emulators pubsub start
```

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.
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

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

```

```java
PubSubOptions options = PubSubOptions.newBuilder()
.setProjectId("my-project-id") // must match project ID specified on remote machine
.setHost("<hostname of machine>:<port>")
.setCredentials(NoCredentials.getInstance())
.build();
PubSub localPubsub = options.getService();
```
3. Point your client to the emulator.
```java
ChannelProvider channelProvider =
// SubscriptionAdminSettings works too.
TopicAdminSettings.defaultChannelProviderBuilder()
.setEndpoint(System.getenv("PUBSUB_EMULATOR_HOST"))
.setCredentialsProvider(
FixedCredentialsProvider.create(NoCredentials.getInstance()))
.build();
TopicAdminClient topicClient = TopicAdminClient.create(
TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build());
Publisher publisher =
Publisher.newBuilder(topicName).setChannelProvider(channelProvider).build();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

```

### Testing code that uses Resource Manager

Expand Down