我的发布服务如下:
public class PubGoogleService {
private static final String TOPIC_ID = "topictest";
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
@Inject
ObjectMapper objectMapper;
private Publisher publisher;
@PostConstruct
private void postConstruct() {
log.info("----------------------------------" + "postConstruct()");
AppIdentityService appIdentityService = AppIdentityServiceFactory.getappIdentityService();
GoogleCredentials credentials = AppEngineCredentials.newBuilder().setappIdentityService(appIdentityService).build();
log.info("----------------------------------" + " projectId=" + PROJECT_ID);
ProjectTopicName topicName = ProjectTopicName.newBuilder()
.setProject(PROJECT_ID)
.setTopic(TOPIC_ID)
.build();
publisher = null;
try {
publisher = Publisher
.newBuilder(topicName)
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
log.info("----------------------------------" + "Created publisher!!!!");
log.info("----------------------------------" + publisher.getTopicNameString());
log.info("----------------------------------" + credentials.toString());
log.info("----------------------------------" + appIdentityService.getServiceaccountName());
} catch (IOException e) {
log.info("It was not possible to create the Pub / Sub publisher of Google.");
log.warning(e.getMessage());
e.printStackTrace();
}
}
public void publish(String message) {
try {
log.info("----------------------------------" + "publish()");
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(message)).build();
log.info("----------------------------------" + pubsubMessage.toString());
ApiFuture<String> future = publisher.publish(pubsubMessage);
} catch (Exception e) {
log.info("----------------------------------" + "e.toString()");
log.info(e.toString());
log.info("----------------------------------" + "e.getMessage()");
log.info(e.getMessage());
}
}
我已授予我在GCP中的服务帐户的发布/订阅管理员权限。
该库的版本为: google-cloud-pubsub 1.100
GCP日志为:
问题是它不会产生任何错误。该消息永远不会在PubSub中发布。我可能做错了什么?