This is part two of the GCP (Google Cloud Platform) PubSub example. Part one is GCP PubSub Send Example. In part one, we learned how to set up GCP admin stuff (e.g. billing), GCP PubSub (e.g. topic), Google Cloud SDK Shell, and a lot more. So if you want to know how to make this example work, please read part one first. I am going straight into the GCP PubSub receive code and will not be discussing about how things are set up. That's covered in part one, please do have a read.
Tools and Materials
These are the tools I used to make this example:
- IntelliJ IDEA 2023.3.4 (Community Edition)
- openjdk-17.0.10
- Spring Boot v3.4.5
- Windows 11
- Google account
- Google Cloud SDK Shell
- Git Bash
The example code is here, github.com/jpllosa/gcp-pubsub. You may grab a copy of it.
Spring Boot GCP PubSub Receive Code
As with part one, GCP PubSub Send Example. There are no changes to our pom.xml. It will have the same dependencies.
- Spring Web (<artifactId>spring-boot-starter-web</artifactId>) - Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.
- Spring Integration (<artifactId>spring-boot-starter-integration</artifactId>, <artifactId>spring-integration-http</artifactId>) - Adds support for Enterprise Integration Patterns. Enables lightweight messaging and supports integration with external systems via declarative adapters.
- Google Cloud Messaging (<artifactId>spring-cloud-gcp-starter-pubsub</artifactId>) - Adds the Google Cloud Support entry and all the required dependencies so that the Google Cloud Pub/Sub integration work out of the box.
Our GCP PubSub message receive code is short and sweet. Just like the send code. We subscribe to a GCP PubSub topic and when a message is receive, we print it out to the console. Here's the code. Explanation follows after.
package com.blogspot.jpllosa;
// imports snipped...
@SpringBootApplication
@RestController
public class GcpPubsubApplication {
// some send code snipped...
@PostMapping("/postMessage")
public RedirectView postMessage(@RequestParam("message") String message) {
this.messagingGateway.sendToGcpPubsub(message);
return new RedirectView("/");
}
// Receive
@Bean
public MessageChannel gcpPubsubInputChannel() {
return new DirectChannel();
}
@Bean
public PubSubInboundChannelAdapter messageChannelAdapter(
@Qualifier("gcpPubsubInputChannel") MessageChannel inputChannel,
PubSubTemplate pubSubTemplate) {
PubSubInboundChannelAdapter adapter =
new PubSubInboundChannelAdapter(pubSubTemplate, "gcp-pubsub-example-sub");
adapter.setOutputChannel(inputChannel);
return adapter;
}
@ServiceActivator(inputChannel = "gcpPubsubInputChannel")
public void messageReceiver(String message) {
System.out.println("Message received: " + message);
}
}
The same with part one, only the GCP PubSub receive bits will be explained. Incoming messages will arrive on the MessageChannel
(DirectChannel
) we created. Simliar to sending, we have an inbound channel adapter that receives messages from GCP PubSub and forwards it to gcpPubsubInputChannel
. The inbound channel adapter is linked to the GCP PubSub subscription name. The adapter is bound and listens for new messages from GCP PubSub gcp-pubsub-example-sub subscription. The method annotated with @ServiceActivator
is called when new messages arrive at gcpPubsubInputChannel
and we simply print the message on the console. Short and sweet.
Spring Boot GCP PubSub Receive in Action
Let's see our code in action. Run the Spring Boot app (e.g. mvnw spring-boot:run
) or thru IntelliJ. When the app is ready, hit the /postMessage
endpoint and you should have something like below printed on the console.
$ curl --data "message=GCP Pubsub send and receive!" localhost:8080/postMessage
We won't be able to see any messages in the GCP PubSub messages section because this happens very quick. If you want to see the messages sent on the messages tab, click PULL to delay the message being consumed. The delay is about 5-ish seconds. It would look like the below.
Spring Boot GCP PubSub Receive Wrap Up
There you have it. Your travel to GCP PubSub has come full circle, assuming you read part one of course. As per usual, we give thanks to Spring for making it easy for us by abstracting whatever it is happening under the hood. Alternatively, you can use other client libaries to connect to GCP PubSub. We created an input channel and attached it to a channel adapter that has subscribed to GCP PubSub. Viola and that is all there is to it! Thank you for reading.