简体   繁体   中英

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. 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:

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.
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)

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

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

Hope it helps.

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

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

For us, when we removed guava dependency, it worked. We are using following version for publishing: com.google.cloud:google-cloud-pubsub:1.33.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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