Skip to content

Commit 9cebf6c

Browse files
authored
Using ServiceOptions.getDefaultProjectId() for project-id (#560)
* Using ServiceOptions.getDefaultProjectId() for project-id
1 parent 9dd4313 commit 9cebf6c

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

pubsub/cloud-client/README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,33 @@
22

33
[Google Cloud Pub/Sub][pubsub] is a fully-managed real-time messaging service that allows you to
44
send and receive messages between independent applications.
5-
These sample Java applications demonstrate how to access the Pub/Sub API using
5+
This sample Java application demonstrates how to access the Pub/Sub API using
66
the [Google Cloud Client Library for Java][google-cloud-java].
77

88
[pubsub]: https://cloud.google.com/pubsub/
99
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
1010

1111
## Quickstart
1212

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

15-
Build your project with:
1617

17-
mvn clean package -DskipTests
18+
gcloud config set project [YOUR PROJECT ID]
1819

19-
## Testing
2020

21-
To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT`
22-
environment variable.
21+
- Build your project with:
2322

24-
export GOOGLE_CLOUD_PROJECT=my-project
2523

26-
Then run the tests with Maven.
24+
mvn clean package -DskipTests
25+
26+
#### Testing
27+
28+
Run the tests with Maven.
2729

2830
mvn clean verify
2931

30-
### Creating a new topic (using the quickstart sample)
32+
#### Creating a new topic (using the quickstart sample)
3133

3234
mvn exec:java -Dexec.mainClass=com.example.pubsub.QuickstartSample

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.9.2-alpha</version>
40+
<version>0.9.4-alpha</version>
4141
</dependency>
4242

4343
<!-- Test dependencies -->

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
// [START pubsub_quickstart]
2020
// Imports the Google Cloud client library
2121

22+
import com.google.cloud.ServiceOptions;
2223
import com.google.cloud.pubsub.spi.v1.PublisherClient;
2324
import com.google.pubsub.v1.TopicName;
2425

2526
public class QuickstartSample {
2627

2728
public static void main(String... args) throws Exception {
2829

30+
// Your Google Cloud Platform project ID
31+
String projectId = ServiceOptions.getDefaultProjectId();
32+
33+
// Your topic ID
34+
String topicId = "my-new-topic";
35+
2936
// Create a new topic
30-
String projectId = args[0];
31-
TopicName topic = TopicName.create(projectId, "my-new-topic");
37+
TopicName topic = TopicName.create(projectId, topicId);
3238
try (PublisherClient publisherClient = PublisherClient.create()) {
3339
publisherClient.createTopic(topic);
3440
}

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

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

21+
import com.google.cloud.ServiceOptions;
2122
import com.google.cloud.pubsub.spi.v1.PublisherClient;
2223
import com.google.pubsub.v1.TopicName;
2324

@@ -40,11 +41,11 @@ public class QuickstartSampleIT {
4041

4142
private ByteArrayOutputStream bout;
4243
private PrintStream out;
43-
private String projectId;
4444

45-
private void deleteTestTopic(String projectId) throws Exception {
45+
private void deleteTestTopic() throws Exception {
4646
try (PublisherClient publisherClient = PublisherClient.create()) {
47-
publisherClient.deleteTopic(TopicName.create(projectId, "my-new-topic"));
47+
publisherClient.deleteTopic(
48+
TopicName.create(ServiceOptions.getDefaultProjectId(), "my-new-topic"));
4849
} catch (IOException e) {
4950
System.err.println("Error deleting topic " + e.getMessage());
5051
}
@@ -55,10 +56,8 @@ public void setUp() {
5556
bout = new ByteArrayOutputStream();
5657
out = new PrintStream(bout);
5758
System.setOut(out);
58-
projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
59-
assertThat(projectId).isNotNull();
6059
try {
61-
deleteTestTopic(projectId);
60+
deleteTestTopic();
6261
} catch (Exception e) {
6362
//empty catch block
6463
}
@@ -67,12 +66,12 @@ public void setUp() {
6766
@After
6867
public void tearDown() throws Exception {
6968
System.setOut(null);
70-
deleteTestTopic(projectId);
69+
deleteTestTopic();
7170
}
7271

7372
@Test
7473
public void testQuickstart() throws Exception {
75-
QuickstartSample.main(projectId);
74+
QuickstartSample.main();
7675
String got = bout.toString();
7776
assertThat(got).contains("my-new-topic created.");
7877
}

0 commit comments

Comments
 (0)