In the previous post, I showed you how to publish a message to Google Pub/Sub topic using Spring Cloud GCP PubSub? In this tutorial, let’s learn together how to subscribe to it to receive messages with Spring Cloud GCP PubSub!
I also created a Spring Boot with GCP Message dependency as follows:
Result:
I will also implement the CommandLineRunner interface for the SpringGcpPubsubSubscribeApplication class as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava.springgcppubsub.subscribe; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringGcpPubsubSubscribeApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(SpringGcpPubsubSubscribeApplication.class, args); } @Override public void run(String... args) throws Exception { } } |
To subscribe to and receive messages from the GCP Pub/Sub topic, we also inject the PubSubTemplate class and use the subscribe() method of this object.
The parameter of this subscribe() method is, respectively, the subscription name associated with the topic and a Consumer object with the type of BasicAcknowledgeablePubsubMessage to consume the message, specifically as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.huongdanjava.springgcppubsub.subscribe; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.google.cloud.spring.pubsub.core.PubSubTemplate; import com.google.pubsub.v1.PubsubMessage; @SpringBootApplication public class SpringGcpPubsubSubscribeApplication implements CommandLineRunner { @Autowired private PubSubTemplate pubSubTemplate; public static void main(String[] args) { SpringApplication.run(SpringGcpPubsubSubscribeApplication.class, args); } @Override public void run(String... args) throws Exception { String subscription = "projects/crested-trainer-341707/subscriptions/huongdanjava-sub"; pubSubTemplate.subscribe(subscription, c -> { PubsubMessage pubsubMessage = c.getPubsubMessage(); System.out.println(pubsubMessage.getData().toStringUtf8()); }); Thread.sleep(100000L); } } |
The data received from the GCP Pub/Sub topic is a PubsubMessage object of the PubSub client library. You can get the exact message content after getData() and call the toStringUtf8() method as I did above.
For the authentication part with GCP Pub/Sub, I will use the spring.cloud.gcp.credentials.location property with the spring.cloud.gcp.project-id property, as follows:
1 2 |
spring.cloud.gcp.project-id=crested-trainer-341707 spring.cloud.gcp.credentials.location=file:/Users/khanh/Downloads/crested-trainer-341707-3c56ce84473e.json |
Result when I publish a message on Google Pub/Sub topic:
as follows:
Normally, after receiving and processing the message from the GCP Pub/Sub topic, without any errors, we will send an acknowledgment for the GCP Pub/Sub topic to delete the message. You can call the ack() method of the BasicAcknowledgeablePubsubMessage object to do this, as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.huongdanjava.springgcppubsub.subscribe; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.google.cloud.spring.pubsub.core.PubSubTemplate; import com.google.pubsub.v1.PubsubMessage; @SpringBootApplication public class SpringGcpPubsubSubscribeApplication implements CommandLineRunner { @Autowired private PubSubTemplate pubSubTemplate; public static void main(String[] args) { SpringApplication.run(SpringGcpPubsubSubscribeApplication.class, args); } @Override public void run(String... args) throws Exception { String subscription = "projects/crested-trainer-341707/subscriptions/huongdanjava-sub"; pubSubTemplate.subscribe(subscription, c -> { PubsubMessage pubsubMessage = c.getPubsubMessage(); System.out.println(pubsubMessage.getData().toStringUtf8()); c.ack(); }); Thread.sleep(100000L); } } |
Now, if you run the example again, you will see that the message that we publish to the topic is only consumed and printed once instead of the two times above: