Skip to content

Commit 57883ae

Browse files
committed
Add PubSub examples, update READMEs and package-info javadoc (googleapis#1075)
1 parent fec0e12 commit 57883ae

File tree

16 files changed

+1119
-21
lines changed

16 files changed

+1119
-21
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This client supports the following Google Cloud Platform services:
1818
- [Google Cloud Compute] (#google-cloud-compute-alpha) (Alpha)
1919
- [Google Cloud Datastore] (#google-cloud-datastore)
2020
- [Google Cloud DNS] (#google-cloud-dns-alpha) (Alpha)
21+
- [Google Cloud Pub/Sub] (#google-cloud-pubsub) (Alpha - Not working on App Engine Standard)
2122
- [Google Cloud Resource Manager] (#google-cloud-resource-manager-alpha) (Alpha)
2223
- [Google Cloud Storage] (#google-cloud-storage)
2324

@@ -62,6 +63,8 @@ Example Applications
6263
- [`Flexible Environment/Datastore example`](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/managed_vms/datastore) - A simple app that uses Cloud Datastore to list the last 10 IP addresses that visited your site.
6364
- Read about how to run the application [here](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managed_vms/README.md).
6465
- [`Flexible Environment/Storage example`](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/managed_vms/cloudstorage) - An app that uploads files to a public Cloud Storage bucket on the App Engine Flexible Environment runtime.
66+
- [`PubSubExample`](./gcloud-java-examples/src/main/java/com/google/cloud/examples/pubsub/PubSubExample.java) - A simple command line interface providing some of Cloud Pub/Sub's functionality
67+
- Read more about using this application on the [`PubSubExample` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/cloud/examples/pubsub/PubSubExample.html).
6568
- [`ResourceManagerExample`](./gcloud-java-examples/src/main/java/com/google/cloud/examples/resourcemanager/ResourceManagerExample.java) - A simple command line interface providing some of Cloud Resource Manager's functionality
6669
- Read more about using this application on the [`ResourceManagerExample` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/cloud/examples/resourcemanager/ResourceManagerExample.html).
6770
- [`SparkDemo`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managed_vms/sparkjava) - An example of using `gcloud-java-datastore` from within the SparkJava and App Engine Flexible Environment frameworks.
@@ -368,6 +371,44 @@ ChangeRequestInfo changeRequest = changeBuilder.build();
368371
zone.applyChangeRequest(changeRequest);
369372
```
370373
374+
Google Cloud Pub/Sub (Alpha)
375+
----------------------
376+
377+
- [API Documentation][pubsub-api]
378+
- [Official Documentation][cloud-pubsub-docs]
379+
380+
#### Preview
381+
382+
Here is a code snippet showing a simple usage example from within Compute Engine/App Engine
383+
Flexible. Note that you must [supply credentials](#authentication) and a project ID if running this
384+
snippet elsewhere. Complete source code can be found at
385+
[CreateSubscriptionAndPullMessages.java](./gcloud-java-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateSubscriptionAndPullMessages.java).
386+
387+
```java
388+
import com.google.cloud.pubsub.Message;
389+
import com.google.cloud.pubsub.PubSub;
390+
import com.google.cloud.pubsub.PubSub.MessageConsumer;
391+
import com.google.cloud.pubsub.PubSub.MessageProcessor;
392+
import com.google.cloud.pubsub.PubSubOptions;
393+
import com.google.cloud.pubsub.Subscription;
394+
import com.google.cloud.pubsub.SubscriptionInfo;
395+
396+
try (PubSub pubsub = PubSubOptions.defaultInstance().service()) {
397+
Subscription subscription =
398+
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
399+
MessageProcessor callback = new MessageProcessor() {
400+
@Override
401+
public void process(Message message) throws Exception {
402+
System.out.printf("Received message \"%s\"%n", message.payloadAsString());
403+
}
404+
};
405+
// Create a message consumer and pull messages (for 60 seconds)
406+
try (MessageConsumer consumer = subscription.pullAsync(callback)) {
407+
Thread.sleep(60_000);
408+
}
409+
}
410+
```
411+
371412
Google Cloud Resource Manager (Alpha)
372413
----------------------
373414
@@ -513,6 +554,7 @@ Apache 2.0 - See [LICENSE] for more information.
513554
[cloud-dns-docs]: https://cloud.google.com/dns/docs
514555
[cloud-dns-activation]: https://console.cloud.google.com/start/api?id=dns
515556
557+
[pubsub-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/cloud/pubsub/package-summary.html
516558
[cloud-pubsub]: https://cloud.google.com/pubsub/
517559
[cloud-pubsub-docs]: https://cloud.google.com/pubsub/docs
518560

TESTING.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,46 @@ Here is an example that clears the dataset created in Step 3.
174174
RemoteBigQueryHelper.forceDelete(bigquery, dataset);
175175
```
176176

177+
### Testing code that uses Pub/Sub
178+
179+
#### On your machine
180+
181+
You can test against a temporary local Pub/Sub by following these steps:
182+
183+
1. Start the local Pub/Sub emulator before running your tests using `LocalPubSubHelper`'s `create`
184+
and `start` methods. This will bind a port for communication with the local Pub/Sub emulator.
185+
```java
186+
LocalPubSubHelper helper = LocalPubSubHelper.create();
187+
188+
helper.start(); // Starts the local Pub/Sub emulator in a separate process
189+
```
190+
191+
2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For
192+
example:
193+
```java
194+
PubSub localPubsub = helper.options().service();
195+
```
196+
197+
3. Run your tests.
198+
199+
4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so:
200+
```java
201+
helper.stop();
202+
```
203+
204+
#### On a remote machine
205+
206+
You can test against a remote Pub/Sub emulator as well. To do this, set the `PubSubOptions` project
207+
endpoint to the hostname of the remote machine, like the example below.
208+
209+
```java
210+
PubSubOptions options = PubSubOptions.builder()
211+
.projectId("my-project-id") // must match project ID specified on remote machine
212+
.host("<hostname of machine>:<port>")
213+
.authCredentials(AuthCredentials.noAuth())
214+
.build();
215+
PubSub localPubsub= options.service();
216+
```
217+
177218
[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts
178219
[create-service-account]:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

gcloud-java-examples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ To run examples from your command line:
100100
mvn exec:java -Dexec.mainClass="com.google.cloud.examples.dns.DnsExample" -Dexec.args="delete some-sample-zone"
101101
```
102102
103+
* Here's an example run of `PubSubExample`.
104+
105+
Before running the example, go to the [Google Developers Console][developers-console] to ensure that "Google Cloud Pub/Sub" is enabled.
106+
```
107+
mvn exec:java -Dexec.mainClass="com.google.cloud.examples.pubsub.PubSubExample" -Dexec.args="create topic test-topic"
108+
mvn exec:java -Dexec.mainClass="com.google.cloud.examples.pubsub.PubSubExample" -Dexec.args="create subscription test-topic test-subscription"
109+
mvn exec:java -Dexec.mainClass="com.google.cloud.examples.pubsub.PubSubExample" -Dexec.args="publish test-topic message1 message2"
110+
mvn exec:java -Dexec.mainClass="com.google.cloud.examples.pubsub.PubSubExample" -Dexec.args="pull sync test-subscription 2"
111+
```
112+
103113
* Here's an example run of `ResourceManagerExample`.
104114
105115
Be sure to change the placeholder project ID "your-project-id" with your own globally unique project ID.

gcloud-java-examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<groupId>org.codehaus.mojo</groupId>
3030
<artifactId>exec-maven-plugin</artifactId>
3131
<configuration>
32+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
3233
<skip>false</skip>
3334
</configuration>
3435
</plugin>

0 commit comments

Comments
 (0)