From b929c858790ef8ccc02163dad162f2b06dfcdc02 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 16 Feb 2017 16:31:02 -0800 Subject: [PATCH 1/3] Upgrading pubsub to 0.9.2-alpha, quick start update --- pubsub/cloud-client/README.md | 16 ++++-- pubsub/cloud-client/pom.xml | 2 +- .../com/example/pubsub/QuickstartSample.java | 49 +++++++++++++------ .../example/pubsub/QuickstartSampleIT.java | 38 +++++++++----- 4 files changed, 72 insertions(+), 33 deletions(-) diff --git a/pubsub/cloud-client/README.md b/pubsub/cloud-client/README.md index 7c6ed464660..07706106ba6 100644 --- a/pubsub/cloud-client/README.md +++ b/pubsub/cloud-client/README.md @@ -15,12 +15,18 @@ Install [Maven](http://maven.apache.org/). Build your project with: mvn clean package -DskipTests - -You can then run a given `ClassName` via: - mvn exec:java -Dexec.mainClass=com.example.pubsub.ClassName \ - -DpropertyName=propertyValue \ - -Dexec.args="any arguments to the app" +## Testing + +To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT` +environment variable. The project should have a dataset named `test_dataset` +with a table named `test_table`. + + export GOOGLE_CLOUD_PROJECT=my-project + +Then run the tests with Maven. + + mvn clean verify ### Creating a new topic (using the quickstart sample) diff --git a/pubsub/cloud-client/pom.xml b/pubsub/cloud-client/pom.xml index d6361384e8f..2881c531f8e 100644 --- a/pubsub/cloud-client/pom.xml +++ b/pubsub/cloud-client/pom.xml @@ -37,7 +37,7 @@ com.google.cloud google-cloud-pubsub - 0.8.0 + 0.9.2-alpha diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java index a4f2492c8c0..7f73b498267 100644 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java +++ b/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java @@ -1,5 +1,5 @@ /* - Copyright 2016, Google, Inc. + 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. @@ -18,23 +18,44 @@ // [START pubsub_quickstart] // Imports the Google Cloud client library -import com.google.cloud.pubsub.PubSub; -import com.google.cloud.pubsub.PubSubOptions; -import com.google.cloud.pubsub.Topic; -import com.google.cloud.pubsub.TopicInfo; -public class QuickstartSample { - public static void main(String... args) throws Exception { - // Instantiates a client - PubSub pubsub = PubSubOptions.getDefaultInstance().getService(); +import com.google.api.gax.core.RpcFuture; +import com.google.cloud.pubsub.spi.v1.Publisher; +import com.google.cloud.pubsub.spi.v1.PublisherClient; +import com.google.protobuf.ByteString; +import com.google.pubsub.v1.PubsubMessage; +import com.google.pubsub.v1.TopicName; - // The name for the new topic - String topicName = "my-new-topic"; +public class QuickstartSample { - // Creates the new topic - Topic topic = pubsub.create(TopicInfo.of(topicName)); + public static void main(String... args) throws Exception { - System.out.printf("Topic %s created.%n", topic.getName()); + // Create a new topic + String projectId = args[0]; + TopicName topic = TopicName.create(projectId, "my-new-topic"); + try (PublisherClient publisherClient = PublisherClient.create()) { + publisherClient.createTopic(topic); + } + System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic()); + + // Creates a publisher + Publisher publisher = null; + try { + publisher = Publisher.newBuilder(topic).build(); + + //Publish a message asynchronously + String message = "my-message"; + ByteString data = ByteString.copyFromUtf8(message); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + RpcFuture messageIdFuture = publisher.publish(pubsubMessage); + + //Print message id of published message + System.out.println("published with message ID: " + messageIdFuture.get()); + } finally { + if (publisher != null) { + publisher.shutdown(); + } + } } } // [END pubsub_quickstart] diff --git a/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java b/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java index 3f9a78642a0..5d9d450796c 100644 --- a/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java +++ b/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java @@ -1,5 +1,5 @@ /* - Copyright 2016, Google, Inc. + 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. @@ -18,8 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.cloud.pubsub.PubSub; -import com.google.cloud.pubsub.PubSubOptions; +import com.google.cloud.pubsub.spi.v1.PublisherClient; +import com.google.pubsub.v1.TopicName; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -27,6 +28,7 @@ import org.junit.runners.JUnit4; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; /** @@ -35,34 +37,44 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class QuickstartSampleIT { + private ByteArrayOutputStream bout; private PrintStream out; + private String projectId; - private static final void deleteTestTopic() { - PubSub pubsub = PubSubOptions.getDefaultInstance().getService(); - String topicName = "my-new-topic"; - pubsub.deleteTopic(topicName); + private void deleteTestTopic(String projectId) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + publisherClient.deleteTopic(TopicName.create(projectId, "my-new-topic")); + } catch (IOException e) { + System.err.println("Error deleting topic " + e.getMessage()); + } } @Before public void setUp() { - deleteTestTopic(); - bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); + projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); + assertThat(projectId).isNotNull(); + try { + deleteTestTopic(projectId); + } catch (Exception e) { + //empty catch block + } } @After - public void tearDown() { + public void tearDown() throws Exception { System.setOut(null); - deleteTestTopic(); + deleteTestTopic(projectId); } @Test public void testQuickstart() throws Exception { - QuickstartSample.main(); + QuickstartSample.main(projectId); String got = bout.toString(); - assertThat(got).contains("Topic my-new-topic created."); + assertThat(got).contains("my-new-topic created."); + assertThat(got).contains("published with message ID"); } } From 1f7434338042731fbb1bc19241f02662254fc162 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 16 Feb 2017 16:45:43 -0800 Subject: [PATCH 2/3] readme fixes, copyright reverting to original --- pubsub/cloud-client/README.md | 4 +--- .../src/main/java/com/example/pubsub/QuickstartSample.java | 2 +- .../src/test/java/com/example/pubsub/QuickstartSampleIT.java | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pubsub/cloud-client/README.md b/pubsub/cloud-client/README.md index 07706106ba6..5a386c13cde 100644 --- a/pubsub/cloud-client/README.md +++ b/pubsub/cloud-client/README.md @@ -19,9 +19,7 @@ Build your project with: ## Testing To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT` -environment variable. The project should have a dataset named `test_dataset` -with a table named `test_table`. - +environment variable. export GOOGLE_CLOUD_PROJECT=my-project Then run the tests with Maven. diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java index 7f73b498267..993df394203 100644 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java +++ b/pubsub/cloud-client/src/main/java/com/example/pubsub/QuickstartSample.java @@ -1,5 +1,5 @@ /* - Copyright 2017, Google, Inc. + Copyright 2016, Google, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java b/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java index 5d9d450796c..59cb5fcbc54 100644 --- a/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java +++ b/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickstartSampleIT.java @@ -1,5 +1,5 @@ /* - Copyright 2017, Google, Inc. + Copyright 2016, Google, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 32dc9f5f890635ccfaf173b562c0816c674ae6ff Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 16 Feb 2017 16:56:44 -0800 Subject: [PATCH 3/3] adding new line in readme --- pubsub/cloud-client/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pubsub/cloud-client/README.md b/pubsub/cloud-client/README.md index 5a386c13cde..c621c2393c2 100644 --- a/pubsub/cloud-client/README.md +++ b/pubsub/cloud-client/README.md @@ -20,6 +20,7 @@ Build your project with: To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT` environment variable. + export GOOGLE_CLOUD_PROJECT=my-project Then run the tests with Maven.