com.google.cloud.pubsub.spi.v1.Publisher.publish没有将数据发送到PubSub [英] com.google.cloud.pubsub.spi.v1.Publisher.publish is not sending data to PubSub

查看:107
本文介绍了com.google.cloud.pubsub.spi.v1.Publisher.publish没有将数据发送到PubSub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

com.google.cloud.pubsub.spi.v1.Publisher.publish(pubsubMessage).get()的较新版本的调用将永远挂起.我不确定是什么问题.

The call to the newer version of com.google.cloud.pubsub.spi.v1.Publisher.publish(pubsubMessage).get() is hanging forever. I'm not sure what the problem is.

代码段:

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!!

在我们这样做的地方,旧的API可以很好地工作:

The older API works fine where we do:

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?

推荐答案

这可能是因为您尚未在GCP控制台中配置该主题的订阅或未授予适当的权限.
必须具有与该主题相关的订阅.另外,请确保您在控制台中提供了正确的权限.请注意,您为此

This may be because you have not configured subscription for the topic or given proper permissions in the GCP console.
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" :(自动生成的电子邮件ID)

"client_email" : (an auto-generated email id)

在控制台中具有管理员订阅者权限的自动生成的电子邮件ID.

auto-generated email id with admin subscriber permissions in the console.

在配置时,您将在projectname.json凭据文件中获得此字段.

You will get this field in your projectname.json credentials file while configuring.

希望有帮助.

这篇关于com.google.cloud.pubsub.spi.v1.Publisher.publish没有将数据发送到PubSub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆