This page describes how to use Pub/Sub in Java applications built with the Spring Framework.
Spring Cloud GCP has several modules for sending messages to Pub/Sub topics and receiving messages from Pub/Sub subscriptions using the Spring Framework. You can use these modules independently or combine them for different use cases:
- Spring Cloud GCP Pub/Sub Starter lets you send and receive messages using helper classes and call the Pub/Sub Java client library for more advanced scenarios.
- Spring Integration Channel Adapters for Pub/Sub let you connect Spring Integration Message Channels with Pub/Sub.
- Spring Cloud Stream Binder for Pub/Sub lets you use Pub/Sub as messaging middleware in Spring Cloud Stream applications.
NOTE: The Spring Cloud GCPLibrary does not provide access to AckReplyConsumerWithResponse, which is a required module to implement the exactly-once feature using the Java client library.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Set up a Google Cloud console project.
Click to:
- Create or select a project.
- Enable the Pub/Sub API for that project.
- Create a service account.
- Download a private key as JSON.
You can view and manage these resources at any time in the Google Cloud console.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. -
Set up a Google Cloud console project.
Click to:
- Create or select a project.
- Enable the Pub/Sub API for that project.
- Create a service account.
- Download a private key as JSON.
You can view and manage these resources at any time in the Google Cloud console.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. - Set the environment variable
GOOGLE_CLOUD_PROJECT
to your Google Cloud project ID.
Using Spring Cloud GCP Pub/Sub Starter
The Spring Cloud GCP Pub/Sub Starter module installs the Pub/Sub Java client library using the Spring Cloud GCP Pub/Sub module. You can call the Pub/Sub API from your Spring application using the classes that the Spring Cloud GCP Pub/Sub Starter provides or the Pub/Sub Java client library. If you're using the classes that the Spring Cloud GCP Pub/Sub Starter provides, you can override the default Pub/Sub configurations.
Installing the module
To install the Spring Cloud GCP Pub/Sub Starter module, add these dependencies
to your pom.xml
file:
The Spring Cloud GCP Pub/Sub Starter artifact:
Supported operations
The Spring Cloud GCP Pub/Sub Starter module includes the following classes:
PubSubAdmin
for administrative operations:- Create topics and subscriptions.
- Get topics and subscriptions.
- List topics and subscriptions.
- Delete topics and subscriptions.
- Get and set acknowledgement deadlines on a subscription.
PubSubTemplate
for sending and receiving messages:- Publish messages to topics.
- Synchronously pull messages from subscriptions.
- Asynchronously pull messages from subscriptions.
- Acknowledge messages.
- Modify acknowledgement deadlines.
- Convert Pub/Sub messages into Plain Old Java Objects (POJOs).
Using Spring Integration channel adapters
If your Spring application uses Spring Integration message channels, you can route messages between your message channels and Pub/Sub using channel adapters.
- An inbound channel adapter forwards messages from a Pub/Sub subscription to a message channel.
- An outbound channel adapter publishes messages from a message channel to a Pub/Sub topic.
Installing the modules
To install modules for Spring Integration channel adapters, add the following to your pom.xml
file:
The Spring Cloud GCP BOM.
The Spring Cloud GCP Pub/Sub Starter and Spring Integration Core artifacts:
Receiving messages from Pub/Sub
To receive messages from a Pub/Sub subscription in your Spring application, use an inbound channel adapter. The inbound channel adapter converts incoming Pub/Sub messages to POJOs and then forwards the POJOs to a message channel.
The example above uses the following Spring beans and Pub/Sub resource:
- A message channel bean named
inputMessageChannel
. - An inbound channel adapter bean named
inboundChannelAdapter
of typePubSubInboundChannelAdapter
. - A Pub/Sub subscription ID named
sub-one
.
The inboundChannelAdapter
asynchronously pulls messages from sub-one
using a PubSubTemplate
and sends the messages to inputMessageChannel
.
The inboundChannelAdapter
sets the acknowledgement mode to MANUAL
so the
application can acknowledge messages after it processes them. The default
acknowledgment mode of PubSubInboundChannelAdapter
types is AUTO
.
The ServiceActivator
bean messageReceiver
logs each message arriving in
inputMessageChannel
to the standard output and then acknowledges the message.
Publishing messages to Pub/Sub
To publish messages from a message channel to a Pub/Sub topic, use an outbound channel adapter. The outbound channel adapter converts POJOs to Pub/Sub messages and then sends the messages to a Pub/Sub topic.
The example above uses the following Spring beans and Pub/Sub resource:
- A message channel bean named
inputMessageChannel
. - An outbound channel adapter bean named
messageSender
of typePubSubMessageHandler
. - A Pub/Sub topic ID named
topic-two
.
The ServiceActivator
bean applies the logic in messageSender
to each
message in inputMessageChannel
.
The PubSubMessageHandler
in messageSender
publishes messages in the
inputMessageChannel
using a PubSubTemplate
. The PubSubMessageHandler
publishes messages to the Pub/Sub topic topic-two
.
Using Spring Cloud Stream Binder
To call the Pub/Sub API in a Spring Cloud Stream application, use the Spring Cloud GCP Pub/Sub Stream Binder module.
Installing the module
To install the Spring Cloud Stream Binder module, add the following to your pom.xml
file:
The Spring Cloud GCP BOM.
The Spring Cloud Stream Binder artifact:
Receiving messages from Pub/Sub
To use your application as an event sink, configure the input binder by specifying the following:
A
Consumer
bean that defines message handling logic. For example, the followingConsumer
bean is namedreceiveMessageFromTopicTwo
:A Pub/Sub topic ID in the configuration file
application.properties
. For example, the following configuration file uses a Pub/Sub topic ID namedtopic-two
:
The example code receives messages from Pub/Sub. The example does the following:
- Finds the Pub/Sub topic ID
topic-two
in the input binding destination inapplication.properties
. - Creates a Pub/Sub subscription to
topic-two
. - Uses the binding name
receiveMessageFromTopicTwo-in-0
to find theConsumer
bean namedreceiveMessageFromTopicTwo
. - Prints incoming messages to the standard output and automatically acknowledges them.
Publishing messages to Pub/Sub
To use your application as an event source, configure the output binder by specifying the following:
A
Supplier
bean that defines where messages come from within your application. For example, the followingSupplier
bean is namedsendMessageToTopicOne
:A Pub/Sub topic ID in the configuration file
application.properties
. For example, the following configuration file uses a Pub/Sub topic ID namedtopic-one
:
The example code publishes messages to Pub/Sub. The example does the following:
- Finds the Pub/Sub topic ID
topic-one
in the output binding destination inapplication.properties
. - Uses the binding name
sendMessageToTopicOne-out-0
to find theSupplier
bean namedsendMessageToTopicOne
. - Sends a numbered message to
topic-one
every 10 seconds.