Spring Integration provides you with a messaging mechanism to exchange Messages through MessageChannels. It uses channel adapters to communicate with external systems.

In this exercise, we will create two apps that communicate using the Spring Integration channel adapters provided by Spring Cloud GCP. These adapters make Spring Integration use Google Cloud Pub/Sub as the message exchange backend.

You will learn how to use Cloud Shell and the Cloud SDK gcloud command.

What you'll learn

What you'll need

Familiarity with standard Linux text editors such as Vim, EMACs or Nano

How will you use use this tutorial?

Read it through onlyRead it and complete the exercises

How would you rate your experience with building HTML/CSS web apps?

NoviceIntermediateProficient

How would you rate your experience with using Google Cloud Platform services?

NoviceIntermediateProficient

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).

Google Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab we will be using Google Cloud Shell, a command line environment running in the Cloud.

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.

Run the following command in Cloud Shell to confirm that you are authenticated:

gcloud auth list

Command output

Credentialed accounts:
- <myaccount>@<mydomain>.com (active)

Note: gcloud is the powerful and unified command-line tool for Google Cloud Platform. Full documentation is available from https://cloud.google.com/sdk/gcloud. It comes pre-installed on Cloud Shell. You will notice its support for tab-completion.

Now let's create our message sending app. Change to the directory of the sending app.

$ cd spring-integration-sender

We want our app to write messages to a channel. After a message is in the channel, it will get picked up by the outbound channel adapter, which converts it from a generic Spring message to a Google Cloud Pub/Sub message and publishes it to a Google Cloud Pub/Sub topic.

In order for our app to write to a channel, we can use a Spring Integration messaging gateway. Using a text editor from vim, emacs or nano, declare a PubsubOutboundGateway interface inside the DemoApplication class.

The @ServiceActivator annotation causes this MessageHandler to be applied to any new messages in inputChannel. In this case, we are calling our outbound channel adapter, PubSubMessageHandler, to publish the message to Google Cloud Pub/Sub's exampleTopic topic.

With the channel adapter in place, we can now auto-wire a PubsubOutboundGateway object and use it to write a message to a channel.

Because of the @PostMapping annotation, we now have an endpoint that is listening to HTTP POST requests, but not without also adding a @RestController annotation to the DemoApplication class to mark it as a REST controller.

The app is listening to POST requests containing a message on port 8080 and endpoint /postMessage, but we will get to this later.

We just created an app that sends messages through Google Cloud Pub/Sub. Now, we will create another app that receives those messages and processes them.

Click + to open a new Cloud Shell session.

Then, in the new Cloud Shell session, change directories to the receiver app's directory:

$ cd spring-integration-receiver

In the previous app, the messaging gateway declaration created the outbound channel for us. Since we don't use a messaging gateway to receive messages, we need to declare our own MessageChannel where incoming messages will arrive.

You set up two Spring Boot apps that use the Spring Integration Channel Adapters for Google Cloud Pub/Sub. They exchange messages among themselves without ever interacting with the Google Cloud Pub/Sub API.

You learned how to use the Spring Integration Channel Adapters for Google Cloud Pub/Sub!