Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading pubsub to 0.9.2-alpha, quick start update #522

Merged
merged 5 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pubsub/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pubsub/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.8.0</version>
<version>0.9.2-alpha</version>
</dependency>

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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]
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@

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;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
Expand All @@ -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");
}
}