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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With