Skip to content

Commit 65e9a24

Browse files
committed
PubSub getting started : initial commit
1 parent ebd29dc commit 65e9a24

File tree

5 files changed

+185
-119
lines changed

5 files changed

+185
-119
lines changed

pubsub/cloud-client/README.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,41 @@ For more samples, see the samples in
1414
## Quickstart
1515

1616
#### Setup
17-
- Install [Maven](http://maven.apache.org/) <p>
17+
- Install [Maven](http://maven.apache.org/)
1818
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/) and run :
19-
20-
21-
gcloud config set project [YOUR PROJECT ID]
22-
23-
19+
```
20+
gcloud config set project [YOUR PROJECT ID]
21+
```
22+
- [Enable](https://console.cloud.google.com/apis/api/pubsub.googleapis.com/overview) Pub/Sub API
2423
- Build your project with:
25-
26-
27-
mvn clean package -DskipTests
24+
```
25+
mvn clean package -DskipTests
26+
```
27+
28+
#### Create a new topic
29+
```
30+
mvn exec:java -Dexec.mainClass=com.example.pubsub.CreateTopicExample -Dexec.args=my-topic-id
31+
```
32+
33+
#### Publish messages
34+
```
35+
mvn exec:java -Dexec.mainClass=com.example.pubsub.PublisherExample -Dexec.args=my-topic-id
36+
```
37+
Publishes 10 messages to the topic `my-topic-id`.
38+
39+
#### Create a subscription
40+
```
41+
mvn exec:java -Dexec.mainClass=com.example.pubsub.CreatePullSubscriptionExample -Dexec.args="my-topic-id my-subscription-id"
42+
```
43+
44+
#### Receive messages
45+
```
46+
mvn exec:java -Dexec.mainClass=com.example.pubsub.SubscriberExample -Dexec.args=my-subscription-id
47+
```
48+
Subscriber will continue to listen on the topic for 5 minutes and print out message id and data as messages are received.
2849

2950
#### Testing
30-
31-
Run the tests with Maven.
32-
33-
mvn clean verify
34-
35-
#### Creating a new topic (using the quickstart sample)
36-
37-
mvn exec:java -Dexec.mainClass=com.example.pubsub.QuickstartSample
51+
Run the test with Maven.
52+
```
53+
mvn clean -Dtest=com.example.pubsub.QuickStartIT verify
54+
```

pubsub/cloud-client/pom.xml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@
2121

2222
<!-- Parent defines config for testing & linting. -->
2323
<parent>
24-
<artifactId>doc-samples</artifactId>
25-
<groupId>com.google.cloud</groupId>
26-
<version>1.0.0</version>
27-
<relativePath>../..</relativePath>
28-
</parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
2929

30-
<properties>
31-
<maven.compiler.target>1.8</maven.compiler.target>
32-
<maven.compiler.source>1.8</maven.compiler.source>
33-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34-
<pubsub.version>0.17.2-alpha</pubsub.version>
35-
</properties>
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<pubsub.version>0.18.0-alpha</pubsub.version>
35+
</properties>
3636

37-
<dependencies>
38-
<dependency>
39-
<groupId>com.google.cloud</groupId>
40-
<artifactId>google-cloud-pubsub</artifactId>
41-
<version>${pubsub.version}</version>
42-
</dependency>
37+
<dependencies>
38+
<dependency>
39+
<groupId>com.google.cloud</groupId>
40+
<artifactId>google-cloud-pubsub</artifactId>
41+
<version>${pubsub.version}</version>
42+
</dependency>
4343

44-
<!-- Test dependencies -->
44+
<!-- Test dependencies -->
4545
<dependency>
4646
<groupId>junit</groupId>
4747
<artifactId>junit</artifactId>
@@ -54,5 +54,10 @@
5454
<version>0.32</version>
5555
<scope>test</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>com.google.guava</groupId>
59+
<artifactId>guava</artifactId>
60+
<version>20.0</version>
61+
</dependency>
5762
</dependencies>
5863
</project>

pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java renamed to pubsub/cloud-client/src/main/java/com/example/pubsub/CreateTopicExample.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@
1616

1717
package com.example.pubsub;
1818

19-
// [START pubsub_quickstart]
19+
// [START pubsub_quickstart_create_topic]
2020
// Imports the Google Cloud client library
21-
2221
import com.google.cloud.ServiceOptions;
2322
import com.google.cloud.pubsub.spi.v1.TopicAdminClient;
2423
import com.google.pubsub.v1.TopicName;
2524

26-
public class QuickstartSample {
25+
public class CreateTopicExample {
2726

2827
public static void main(String... args) throws Exception {
2928

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

33-
// Your topic ID
34-
String topicId = "my-new-topic";
32+
// Your topic ID, eg. "my-topic-id"
33+
String topicId = args[0];
3534

3635
// Create a new topic
3736
TopicName topic = TopicName.create(projectId, topicId);
@@ -42,4 +41,4 @@ public static void main(String... args) throws Exception {
4241
System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic());
4342
}
4443
}
45-
// [END pubsub_quickstart]
44+
// [END pubsub_quickstart_create_topic]
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright 2016, Google, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.pubsub;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.ServiceOptions;
22+
import com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient;
23+
import com.google.cloud.pubsub.spi.v1.TopicAdminClient;
24+
import com.google.pubsub.v1.SubscriptionName;
25+
import com.google.pubsub.v1.TopicName;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
import org.junit.rules.Timeout;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
import java.io.ByteArrayOutputStream;
35+
import java.io.IOException;
36+
import java.io.PrintStream;
37+
import java.util.List;
38+
39+
/** Tests for quickstart sample. */
40+
@RunWith(JUnit4.class)
41+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
42+
public class QuickStartIT {
43+
44+
private ByteArrayOutputStream bout;
45+
private PrintStream out;
46+
47+
private String projectId = ServiceOptions.getDefaultProjectId();
48+
private String topicId = formatForTest("my-topic-id");
49+
private String subscriptionId = formatForTest("my-subscription-id");
50+
51+
@Rule public Timeout globalTimeout = Timeout.seconds(300); // 5 minute timeout
52+
53+
@Before
54+
public void setUp() {
55+
bout = new ByteArrayOutputStream();
56+
out = new PrintStream(bout);
57+
System.setOut(out);
58+
try {
59+
deleteTestSubscription();
60+
deleteTestTopic();
61+
} catch (Exception e) {
62+
// topic, subscription may not yet exist
63+
}
64+
}
65+
66+
@After
67+
public void tearDown() throws Exception {
68+
System.setOut(null);
69+
deleteTestSubscription();
70+
deleteTestTopic();
71+
}
72+
73+
@Test
74+
public void testQuickstart() throws Exception {
75+
//create a topic
76+
CreateTopicExample.main(topicId);
77+
String got = bout.toString();
78+
assertThat(got).contains(topicId + " created.");
79+
80+
// publish messages
81+
List<String> published = PublisherExample.publishMessages(topicId);
82+
assertThat(published).hasSize(5);
83+
84+
// create a subscriber
85+
CreatePullSubscriptionExample.main(topicId, subscriptionId);
86+
got = bout.toString();
87+
assertThat(got).contains(subscriptionId + " created.");
88+
89+
SubscriberExample subscriberExample = new SubscriberExample(subscriptionId);
90+
// receive messages
91+
Thread subscriberThread = new Thread(subscriberExample);
92+
subscriberThread.start();
93+
94+
List<String> received;
95+
while ((received = subscriberExample.getReceivedMessages()).size() < 5) {
96+
Thread.sleep(1000);
97+
}
98+
assertThat(received).containsAllIn(published);
99+
subscriberExample.stopSubscriber();
100+
subscriberThread.join();
101+
}
102+
103+
private String formatForTest(String name) {
104+
return name + "-" + java.util.UUID.randomUUID().toString();
105+
}
106+
107+
private void deleteTestTopic() throws Exception {
108+
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
109+
topicAdminClient.deleteTopic(TopicName.create(projectId, topicId));
110+
} catch (IOException e) {
111+
System.err.println("Error deleting topic " + e.getMessage());
112+
}
113+
}
114+
115+
private void deleteTestSubscription() throws Exception {
116+
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
117+
subscriptionAdminClient.deleteSubscription(
118+
SubscriptionName.create(projectId, subscriptionId));
119+
} catch (IOException e) {
120+
System.err.println("Error deleting subscription " + e.getMessage());
121+
}
122+
}
123+
}

pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)