Skip to content

Commit 9f624a0

Browse files
author
chenyumic
authored
Update CreateTopicAndPublishMessages.java
1 parent dd56e87 commit 9f624a0

1 file changed

Lines changed: 2 additions & 9 deletions

File tree

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,8 @@ public static void publishMessages() throws Exception {
8383
public static void publishMessagesWithErrorHandler() throws Exception {
8484
// [START pubsub_publish_error_handler]
8585
ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id");
86-
Publisher publisher = null;
8786

88-
try {
89-
// Create a publisher instance with default settings bound to the topic
90-
publisher = Publisher.newBuilder(topicName).build();
87+
try (Publisher publisher = Publisher.newBuilder(topicName).build()) {
9188

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

@@ -119,17 +116,13 @@ public void onSuccess(String messageId) {
119116
}
120117
});
121118
}
122-
} finally {
123-
if (publisher != null) {
124-
// When finished with the publisher, shutdown to free up resources.
125-
publisher.shutdown();
126-
}
127119
}
128120
// [END pubsub_publish_error_handler]
129121
}
130122

131123
public static void main(String... args) throws Exception {
132124
createTopic();
133125
publishMessages();
126+
publishMessagesWithErrorHandler();
134127
}
135128
}

0 commit comments

Comments
 (0)