Skip to content

Commit f9ea543

Browse files
authored
Merge pull request GoogleCloudPlatform#522 from GoogleCloudPlatform/pubsub
Upgrading pubsub to 0.9.2-alpha, quick start update
2 parents 20ad2ed + 32dc9f5 commit f9ea543

File tree

4 files changed

+69
-31
lines changed

4 files changed

+69
-31
lines changed

pubsub/cloud-client/README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ Install [Maven](http://maven.apache.org/).
1515
Build your project with:
1616

1717
mvn clean package -DskipTests
18-
19-
You can then run a given `ClassName` via:
2018

21-
mvn exec:java -Dexec.mainClass=com.example.pubsub.ClassName \
22-
-DpropertyName=propertyValue \
23-
-Dexec.args="any arguments to the app"
19+
## Testing
20+
21+
To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT`
22+
environment variable.
23+
24+
export GOOGLE_CLOUD_PROJECT=my-project
25+
26+
Then run the tests with Maven.
27+
28+
mvn clean verify
2429

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

pubsub/cloud-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<dependency>
3838
<groupId>com.google.cloud</groupId>
3939
<artifactId>google-cloud-pubsub</artifactId>
40-
<version>0.8.0</version>
40+
<version>0.9.2-alpha</version>
4141
</dependency>
4242

4343
<!-- Test dependencies -->

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

+34-13
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,44 @@
1818

1919
// [START pubsub_quickstart]
2020
// Imports the Google Cloud client library
21-
import com.google.cloud.pubsub.PubSub;
22-
import com.google.cloud.pubsub.PubSubOptions;
23-
import com.google.cloud.pubsub.Topic;
24-
import com.google.cloud.pubsub.TopicInfo;
2521

26-
public class QuickstartSample {
27-
public static void main(String... args) throws Exception {
28-
// Instantiates a client
29-
PubSub pubsub = PubSubOptions.getDefaultInstance().getService();
22+
import com.google.api.gax.core.RpcFuture;
23+
import com.google.cloud.pubsub.spi.v1.Publisher;
24+
import com.google.cloud.pubsub.spi.v1.PublisherClient;
25+
import com.google.protobuf.ByteString;
26+
import com.google.pubsub.v1.PubsubMessage;
27+
import com.google.pubsub.v1.TopicName;
3028

31-
// The name for the new topic
32-
String topicName = "my-new-topic";
29+
public class QuickstartSample {
3330

34-
// Creates the new topic
35-
Topic topic = pubsub.create(TopicInfo.of(topicName));
31+
public static void main(String... args) throws Exception {
3632

37-
System.out.printf("Topic %s created.%n", topic.getName());
33+
// Create a new topic
34+
String projectId = args[0];
35+
TopicName topic = TopicName.create(projectId, "my-new-topic");
36+
try (PublisherClient publisherClient = PublisherClient.create()) {
37+
publisherClient.createTopic(topic);
38+
}
39+
System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic());
40+
41+
// Creates a publisher
42+
Publisher publisher = null;
43+
try {
44+
publisher = Publisher.newBuilder(topic).build();
45+
46+
//Publish a message asynchronously
47+
String message = "my-message";
48+
ByteString data = ByteString.copyFromUtf8(message);
49+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
50+
RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
51+
52+
//Print message id of published message
53+
System.out.println("published with message ID: " + messageIdFuture.get());
54+
} finally {
55+
if (publisher != null) {
56+
publisher.shutdown();
57+
}
58+
}
3859
}
3960
}
4061
// [END pubsub_quickstart]

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

+24-12
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21-
import com.google.cloud.pubsub.PubSub;
22-
import com.google.cloud.pubsub.PubSubOptions;
21+
import com.google.cloud.pubsub.spi.v1.PublisherClient;
22+
import com.google.pubsub.v1.TopicName;
23+
2324
import org.junit.After;
2425
import org.junit.Before;
2526
import org.junit.Test;
2627
import org.junit.runner.RunWith;
2728
import org.junit.runners.JUnit4;
2829

2930
import java.io.ByteArrayOutputStream;
31+
import java.io.IOException;
3032
import java.io.PrintStream;
3133

3234
/**
@@ -35,34 +37,44 @@
3537
@RunWith(JUnit4.class)
3638
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3739
public class QuickstartSampleIT {
40+
3841
private ByteArrayOutputStream bout;
3942
private PrintStream out;
43+
private String projectId;
4044

41-
private static final void deleteTestTopic() {
42-
PubSub pubsub = PubSubOptions.getDefaultInstance().getService();
43-
String topicName = "my-new-topic";
44-
pubsub.deleteTopic(topicName);
45+
private void deleteTestTopic(String projectId) throws Exception {
46+
try (PublisherClient publisherClient = PublisherClient.create()) {
47+
publisherClient.deleteTopic(TopicName.create(projectId, "my-new-topic"));
48+
} catch (IOException e) {
49+
System.err.println("Error deleting topic " + e.getMessage());
50+
}
4551
}
4652

4753
@Before
4854
public void setUp() {
49-
deleteTestTopic();
50-
5155
bout = new ByteArrayOutputStream();
5256
out = new PrintStream(bout);
5357
System.setOut(out);
58+
projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
59+
assertThat(projectId).isNotNull();
60+
try {
61+
deleteTestTopic(projectId);
62+
} catch (Exception e) {
63+
//empty catch block
64+
}
5465
}
5566

5667
@After
57-
public void tearDown() {
68+
public void tearDown() throws Exception {
5869
System.setOut(null);
59-
deleteTestTopic();
70+
deleteTestTopic(projectId);
6071
}
6172

6273
@Test
6374
public void testQuickstart() throws Exception {
64-
QuickstartSample.main();
75+
QuickstartSample.main(projectId);
6576
String got = bout.toString();
66-
assertThat(got).contains("Topic my-new-topic created.");
77+
assertThat(got).contains("my-new-topic created.");
78+
assertThat(got).contains("published with message ID");
6779
}
6880
}

0 commit comments

Comments
 (0)