简体   繁体   English

com.google.cloud.pubsub.spi.v1.Publisher.publish 未向 PubSub 发送数据

[英]com.google.cloud.pubsub.spi.v1.Publisher.publish is not sending data to PubSub

The call to the newer version of com.google.cloud.pubsub.spi.v1.Publisher.publish(pubsubMessage).get() is hanging forever.com.google.cloud.pubsub.spi.v1.Publisher.publish(pubsubMessage).get()的较新版本的调用永远挂起。 I'm not sure what the problem is.我不确定是什么问题。

Code snippet:代码片段:

com.google.cloud.pubsub.spi.v1.Publisher publisher = Publisher.defaultBuilder(TopicName.parse("projects/" + projectId + "/topics/" + topicName))
            .setChannelProvider(TopicAdminSettings
                    .defaultChannelProviderBuilder()
                    .setCredentialsProvider(FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream(keyFile))))
                    .build())
            .build();
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
messageIdFuture.get() // HANGS FOREVER!!

The older API works fine where we do:旧的 API 在我们做的地方工作得很好:

GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(new NetHttpTransport())
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(serviceAccount)
            .setServiceAccountScopes(Arrays.asList(PubsubScopes.PUBSUB))
            .setServiceAccountPrivateKeyFromP12File(new File(keyFile))
            .build();

Pubsub pusub =  new Pubsub.Builder(transport, JSON_FACTORY, credential).setApplicationName("bigquery").build();

PubsubMessage pubsubMessage = new PubsubMessage();
pubsubMessage.encodeData(message.getBytes());

PublishRequest publishRequest = new PublishRequest();
publishRequest.setMessages(Arrays.asList(pubsubMessage));
pubsub.projects().topics().publish(outputTopic, publishRequest).execute();

Can somebody point out what am I missing?有人可以指出我错过了什么吗?

This may be because you have not configured subscription for the topic or given proper permissions in the GCP console. 这可能是因为您尚未为主题配置订阅或在GCP控制台中给予适当的权限。
It is required to have a subscription attached to the topic. 需要附加主题的订阅。 Also make sure you give the correct permissions in the console. 还要确保在控制台中提供正确的权限。 Note that you give this 请注意,你给这个

"client_email" : (an auto-generated email id) “client_email”:(自动生成的电子邮件ID)

auto-generated email id with admin subscriber permissions in the console. 控制台中具有管理员订阅者权限的自动生成的电子邮件ID。

You will get this field in your projectname.json credentials file while configuring. 配置时,您将在projectname.json凭证文件中获取此字段。

Hope it helps. 希望能帮助到你。

我不知道为什么,但是一旦我为guava添加了一个编译依赖项,就不再在get调用上挂起了。

compile group: 'com.google.guava', name: 'guava', version: '23.0'

For us, when we removed guava dependency, it worked.对我们来说,当我们删除 guava 依赖时,它起作用了。 We are using following version for publishing: com.google.cloud:google-cloud-pubsub:1.33.0我们使用以下版本进行发布:com.google.cloud:google-cloud-pubsub:1.33.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM