Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
// [START pubsub_quickstart]
// Imports the Google Cloud client library

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;

public class QuickstartSample {
Expand All @@ -36,26 +32,8 @@ public static void main(String... args) throws Exception {
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();
}
}
System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic());
}
}
// [END pubsub_quickstart]
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ public void testQuickstart() throws Exception {
QuickstartSample.main(projectId);
String got = bout.toString();
assertThat(got).contains("my-new-topic created.");
assertThat(got).contains("published with message ID");
}
}