Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 650
figaro Avatar asked Nov 08 '22 21:11

figaro


1 Answers

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.

like image 178
Abhishek Raj Avatar answered Nov 22 '22 09:11

Abhishek Raj