我跟着GAE pub-sub客户端示例代码来实现发布消息。它工作了一天。
但它今天停止发布任何消息。我使用与https://cloud.google.com/pubsub/docs/publisher中的示例代码类似的pub / sub客户端代码。
如果我通过https://console.cloud.google.com发布有关该主题的消息,我的客户端可以毫无问题地收到消息。
发布代码有什么问题或我需要吗?
public static void publish(String message) throws Exception {
TopicName topicName = TopicName.of(projectId, topicId);
Publisher publisher = null;
List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
try {
publisher = Publisher.newBuilder(topicName).build();
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage =
PubsubMessage.newBuilder().setData(data).build();
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
messageIdFutures.add(messageIdFuture);
}finally {
List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
for (String messageId : messageIds) {
System.out.println("published with message ID: " + messageId);
}
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}