Skip to content

Commit

Permalink
Update CreateTopicAndPublishMessages.java
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyumic authored May 17, 2018
1 parent dd56e87 commit 9f624a0
Showing 1 changed file with 2 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ public static void publishMessages() throws Exception {
public static void publishMessagesWithErrorHandler() throws Exception {
// [START pubsub_publish_error_handler]
ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id");
Publisher publisher = null;

try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.newBuilder(topicName).build();
try (Publisher publisher = Publisher.newBuilder(topicName).build()) {

List<String> messages = Arrays.asList("first message", "second message");

Expand Down Expand Up @@ -119,17 +116,13 @@ public void onSuccess(String messageId) {
}
});
}
} finally {
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}
// [END pubsub_publish_error_handler]
}

public static void main(String... args) throws Exception {
createTopic();
publishMessages();
publishMessagesWithErrorHandler();
}
}

0 comments on commit 9f624a0

Please sign in to comment.