Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to access PubSub using java client

Hi folks,

I want to use GCP PubSub using custom java client but I`m unable to connect.

I have use below code to connect - 

 
TopicName topicName = TopicName.of(projectId, topicId);
Publisher publisher = null;
publisher = Publisher.newBuilder(topicName).build();
ByteString data = ByteString.copyFromUtf8(sampleJsonMessage);
PubsubMessage pubSubMessage = PubsubMessage.newBuilder().setData(data).build();
ApiFuture<String> messageIdFuture = publisher.publish(pubSubMessage);
 
But Its not working. Any suggestion to achieve this.
 

Also I have tried https://cloud.google.com/pubsub/docs/publish-receive-messages-client-library this example as well, but getting below exception -

BabaHere_0-1694171276552.png

 

 

 
Thanks
1 0 871
0 REPLIES 0
Top Labels in this Space
Top Solution Authors