Spring framework is very popular and most widely used across the world. It is also one of the oldest frameworks in Java. The Spring Cloud GCP project makes the Spring Framework a first-class citizen of the Google Cloud Platform (GCP).
In this blog series, we will create an application to send messages and another for receiving messages. We will use Google Cloud Pub/Sub as the underlying messaging system. To integrate Google Cloud Pub/Sub with our application we will be using Spring.
Configurations in Google Cloud Platform
Basic GCP setup
Firstly, to get started, you will require an active GCP account with billing enabled. Please navigate to console.cloud.google.com.
If you don’t have an account, please create an account with your new or existing Google account and start the free trial.

Next, you have to create a project under which you will configure and provision all the resources required. You can find the Projects Tab right next to the GCP logo. Please do remember the project ID/name, which is unique across all Google Cloud projects universally.
Now you will have to activate the cloud shell. To activate it please click on the icon mentioned below.

Run the following command to check if your project is configured properly:
gcloud config list project
It will show the current project name that you set in the previous step. If it does not set your project using the following command:
gcloud config set project <PROJECT_ID>
Pub/Sub Configurations
Now we will configure the Pub/Sub resources. First, go to the search bar and open the Pub/Sub service or just follow this link:
https://console.cloud.google.com/cloudpubsub
Note: For first-time users, you might need to enable the API.
Now, let’s start by first creating a topic.

As shown in the image, go to the to topics and click on “Create Topic”. Then the following Dialogue box will be shown.

Choose a name for your topic and create the topic.
Then on the topics page click on the three-dot menu beside your topic name.

Give it a name and create a subscription.
Creating Spring Project
Visit Spring Initializr in your browser and create a project as shown below

Click on “Generate”, and it will download the project to your machine. Then, open the project in your favorite IDE.
Also, download the same Spring Project into GCP using the cloud shell. Run the following command:
curl https://start.spring.io/starter.tgz \
-d bootVersion=2.4.7 \
-d dependencies=web,integration,cloud-gcp-pubsub \
-d baseDir=spring-integration-sender | tar -xzvf -
This will download the message sender application. Next, run this command:
curl https://start.spring.io/starter.tgz \
-d bootVersion=2.4.7 \
-d dependencies=web,integration,cloud-gcp-pubsub \
-d baseDir=spring-integration-receiver | tar -xzvf -
This will download the message receiver application.
We downloaded this so that we would be able to run the app from GCP after we are done creating the app in our IDE.
Creating the sender app
We want our app to write messages to a channel. After that, the outbound channel adapter will pick up the message, and convert it from a generic Spring message to a Google Cloud Pub/Sub message. Then it publishes the message to a topic.
To write messages to a channel from our app we will use the Spring Integration messaging gateway.
In your IDE, navigate to the main application class in your src folder.

Then, define the messaging gateway by adding the piece of code mentioned below:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.integration.annotation.MessagingGateway;
@SpringBootApplication
public class SpringCloudMessagingSenderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudMessagingSenderApplication.class, args);
}
@MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
public interface PubsubOutboundGateway {
void sendToPubsub(String text);
}
}
After that, we will define an outbound channel adapter to consume the messages in the channel. And then, publish them to a Pub/Sub topic.
Please add the following piece of code to your app and add their specific imports:
@Bean
@ServiceActivator(inputChannel = "pubsubOutputChannel")
public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
return new PubSubMessageHandler(pubsubTemplate, "my-first-topic");
}
Here, we are calling our outbound channel adapter, to publish the message to “my-first-topic”.
Now we can auto-wire PubsubOutboundGateway
object and then use it to write a message to a channel.
@Autowired
private PubsubOutboundGateway messagingGateway;
@PostMapping("/postMessage")
public RedirectView postMessage(@RequestParam("message") String message) {
this.messagingGateway.sendToPubsub(message);
return new RedirectView("/");
}
We will also have to add a @RestController
annotation to the main application class to make it a Rest Controller. Now we have an endpoint that is listening to the POST requests.
This app will listen to POST requests having a message on port 8080/postMessage
endpoint.
Running the app
Go to the cloud shell. Then set the project ID in the environment variable:
export GOOGLE_CLOUD_PROJECT=`gcloud config list --format 'value(core.project)'`
Then run the app:
./mvnw spring-boot:run
Summary
In this blog, we looked at creating a Spring app that can send messages using Google Cloud Pub/Sub. In the next blog in the series, we will be continuing with creating the receiver app. Stay Tuned.
For more awesome Tech Blogs, like this one, check out Knoldus Blogs.
References
All the contents in this blog are referred from Google Cloud Platform and Spring documentation and guides.