diff --git a/pubsub/cloud-client/README.md b/pubsub/cloud-client/README.md index 7c6ed464660..c621c2393c2 100644 --- a/pubsub/cloud-client/README.md +++ b/pubsub/cloud-client/README.md @@ -15,12 +15,17 @@ 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. + + 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..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 @@ -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..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 @@ -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"); } }