Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pubsub getting started #684

Merged
merged 25 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1435445
initial commit iap samples
jabubake May 28, 2017
ee46851
adding test
jabubake May 28, 2017
b491208
cleanup
jabubake May 28, 2017
dca4a02
license update
jabubake May 28, 2017
0327851
readme cleanup
jabubake May 28, 2017
79af7d6
license cleanup
jabubake May 30, 2017
64bf09a
Merge branch 'master' into iap_samples
jabubake May 30, 2017
e242a82
pom.xml cleanup
jabubake May 30, 2017
7a13ebc
using new tooling
jabubake May 30, 2017
ebd29dc
adding doc tags
jabubake May 30, 2017
65e9a24
PubSub getting started : initial commit
jabubake May 31, 2017
f3176bc
getting started guide files
jabubake May 31, 2017
3f57f82
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake May 31, 2017
e53c182
formatting fixes
jabubake May 31, 2017
011c88f
create subscription before publishing a message
jabubake May 31, 2017
9404a36
simplifying publisher, subscriber snippets
jabubake Jun 19, 2017
b9038c9
Merge remote-tracking branch 'origin/master' into pubsub_getting_started
jabubake Jun 19, 2017
e2a3302
updates
jabubake Jun 19, 2017
5129e14
remove unused code
jabubake Jun 19, 2017
48ecccc
removing unused imports
jabubake Jun 20, 2017
952f001
formatting updates
jabubake Jul 12, 2017
34f57fa
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake Jul 31, 2017
3e59626
pub/sub getting started
jabubake Jul 31, 2017
e6ec754
update README instructions
jabubake Jul 31, 2017
4093499
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake Aug 7, 2017
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
50 changes: 33 additions & 17 deletions pubsub/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,40 @@ For more samples, see the samples in
## Quickstart

#### Setup
- Install [Maven](http://maven.apache.org/) <p>
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/) and run :


gcloud config set project [YOUR PROJECT ID]

- Install [Maven](http://maven.apache.org/).
- [Enable](https://console.cloud.google.com/apis/api/pubsub.googleapis.com/overview) Pub/Sub API.
- Set up [authentication](https://cloud.google.com/docs/authentication/getting-started).

#### Build
- Build your project with:


mvn clean package -DskipTests
```
mvn clean package -DskipTests
```

#### Create a new topic
```
mvn exec:java -Dexec.mainClass=com.example.pubsub.CreateTopicExample -Dexec.args=my-topic
```

#### Create a subscription
```
mvn exec:java -Dexec.mainClass=com.example.pubsub.CreatePullSubscriptionExample -Dexec.args="my-topic-id my-sub"
```

#### Publish messages
```
mvn exec:java -Dexec.mainClass=com.example.pubsub.PublisherExample -Dexec.args=my-topic
```
Publishes 5 messages to the topic `my-topic`.

#### Receive messages
```
mvn exec:java -Dexec.mainClass=com.example.pubsub.SubscriberExample -Dexec.args=my-sub
```
Subscriber will continue to listen on the topic for 5 minutes and print out message id and data as messages are received.

#### Testing

Run the tests with Maven.

mvn clean verify

#### Creating a new topic (using the quickstart sample)

mvn exec:java -Dexec.mainClass=com.example.pubsub.QuickstartSample
Run the test with Maven.
```
mvn verify
```
13 changes: 1 addition & 12 deletions pubsub/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,16 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<pubsub.version>0.21.1-beta</pubsub.version>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>${pubsub.version}</version>
<version>0.21.0-beta</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2017, Google, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.example.pubsub;

// [START pubsub_quickstart_create_subscription]
import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.PushConfig;
import com.google.pubsub.v1.Subscription;
import com.google.pubsub.v1.SubscriptionName;
import com.google.pubsub.v1.TopicName;

public class CreatePullSubscriptionExample {

/**
* Create a pull subscription.
*
* @param args topic subscriptionId
* @throws Exception exception thrown if operation is unsuccessful
*/
public static void main(String... args) throws Exception {

// Your Google Cloud Platform project ID
String projectId = ServiceOptions.getDefaultProjectId();

// Your topic ID, eg. "my-topic"
String topicId = args[0];

// Your subscription ID eg. "my-sub"
String subscriptionId = args[1];

TopicName topicName = TopicName.create(projectId, topicId);

// Create a new subscription
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than throw an exception (above), shouldn't we catch them and explain why they might get one and what to do if they do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// create a pull subscription with default acknowledgement deadline (= 10 seconds)
Subscription subscription =
subscriptionAdminClient.createSubscription(
subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
}

System.out.printf(
"Subscription %s:%s created.\n",
subscriptionName.getProject(), subscriptionName.getSubscription());
}
}
// [END pubsub_quickstart_create_subscription]
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@

package com.example.pubsub;

// [START pubsub_quickstart]
// [START pubsub_quickstart_create_topic]
// Imports the Google Cloud client library

import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.pubsub.v1.TopicName;

public class QuickstartSample {
public class CreateTopicExample {

/**
* Create a topic.
*
* @param args topicId
* @throws Exception exception thrown if operation is unsuccessful
*/
public static void main(String... args) throws Exception {

// Your Google Cloud Platform project ID
String projectId = ServiceOptions.getDefaultProjectId();

// Your topic ID
String topicId = "my-new-topic";
// Your topic ID, eg. "my-topic"
String topicId = args[0];

// Create a new topic
TopicName topic = TopicName.create(projectId, topicId);
Expand All @@ -42,4 +47,4 @@ public static void main(String... args) throws Exception {
System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic());
}
}
// [END pubsub_quickstart]
// [END pubsub_quickstart_create_topic]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.pubsub;
// [START pubsub_quickstart_publisher]

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
import java.util.ArrayList;
import java.util.List;

public class PublisherExample {

static final int MESSAGE_COUNT = 5;

// use the default project id
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();

//schedule a message to be published, messages are automatically batched
private static ApiFuture<String> publishMessage(Publisher publisher, String message)
throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than throwing an exception, shouldn't we catch them? and explain what's going on?

// convert message to bytes
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
return publisher.publish(pubsubMessage);
}

/** Publish messages to a topic. */
public static void main(String... args) throws Exception {
// topic id, eg. "my-topic"
String topicId = args[0];
TopicName topicName = TopicName.create(PROJECT_ID, topicId);
Publisher publisher = null;
List<ApiFuture<String>> apiFutures = new ArrayList<>();
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.defaultBuilder(topicName).build();
for (int i = 0; i < MESSAGE_COUNT; i++) {
String message = "message-" + i;
ApiFuture<String> messageId = publishMessage(publisher, message);
apiFutures.add(messageId);
}
} finally {
// Once published, returns server-assigned message ids (unique within the topic)
List<String> messageIds = ApiFutures.allAsList(apiFutures).get();
for (String messageId : messageIds) {
System.out.println(messageId);
}
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}
}
}
// [END pubsub_quickstart_quickstart]
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.pubsub;

// [START pubsub_quickstart_subscriber]

import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.SubscriptionName;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class SubscriberExample {

// use the default project id
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();

private static final BlockingQueue<PubsubMessage> messages = new LinkedBlockingDeque<>();

static class MessageReceiverExample implements MessageReceiver {

@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
messages.offer(message);
consumer.ack();
}
}

/** Receive messages over a subscription. */
public static void main(String... args) throws Exception {
// set subscriber id, eg. my-sub
String subscriptionId = args[0];
SubscriptionName subscriptionName = SubscriptionName.create(PROJECT_ID, subscriptionId);
Subscriber subscriber = null;
try {
// create a subscriber bound to the asynchronous message receiver
subscriber =
Subscriber.defaultBuilder(subscriptionName, new MessageReceiverExample()).build();
subscriber.startAsync().awaitRunning();
// Continue to listen to messages
while (true) {
PubsubMessage message = messages.take();
System.out.println("Message Id: " + message.getMessageId());
System.out.println("Data: " + message.getData().toStringUtf8());
}
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
}
// [END pubsub_quickstart_subscriber]
Loading