Publish And Subscribe Message Operation: the big hope for Spring Cloud GCP.

Reading Time: 3 minutes

Pre-Requisite Basic of GCP Pub-Sub and Overview of Our Blog written by knolder.

GCP Pub-Sub Architecture diagram
(Above sampled image is used from Google Docs.)

What is GCP Pub-Sub

Google Cloud Pub/Sub is an asynchronous messaging carrier based on Google’s own infrastructure issue that has been relied on with the aid of many Google products for over a decade. Google merchandise, together with advertisements, seek, and Gmail, Travers extra than 500 million messages in keeping with second using this infrastructure, imparting a complete of more than 1 TB/s of records. This is Google’s very own implementation of Apache Kafka and is fully managed by Google. which makes it tremendously reliable and scalable.

Overview of Pub/Sub Operation

PubSub Operations is a separation that lets Spring customers to apply Google Cloud Pub/Sub without depending on any Google Cloud Pub/Sub API semantics. It affords the not unusual set of operations needed to have interaction with Google Cloud Pub/Sub.

PubSub Template is the default fulfillment of PubSub Operations and it makes use of the Google Cloud Java consumer for Pub/Sub to have interaction with Google Cloud Pub/Sub.

The two parts of our understanding are Pub (publish a message) And Sub (Subscribing to subscription/Topics)

1. Publish Message over GCP Network

PubSubTemplate affords asynchronous methods to put up messages to a Google Cloud Pub/Sub subject matter. The publish() method has parameters input a Topic name(Mentioned in the service account) to Publish the message to, a payload of a familiar kind, and, alternative, a map with the message headers. the topic call ought to both be a canonical topic name within the modern-day assignment, or the fully-certified name relating to a topic in a unique project the use of the projects/<project_name>/topics/<topic_name> format.

Here is an example of how to publish a message to a Google Cloud Pub/Sub topic:

Map<String, String> headers = Collections.singletonMap("key1", "val1");
pubSubTemplate.publish(topicName, "messagePayloadToBePublished", headers).get();
2. Subscribing to subscription/Topics

Google Cloud Pub/Sub allows you to associate multiple subscriptions to the same topic. PubSubTemplate allows you to focus on subscriptions with the scribe() method. Upon obtaining a subscription, messages can be retrieved asynchronously from Google Cloud Pub/Sub and delivered to a consumer-supplied message manager. The subscription invocation is also a fully qualified subscription Therefore, name within the last assignment. A fully qualified subscription name in a unique assignment using projects/<project_name>/subscriptions/<subscription_name>

example: Subscribe to a subscription with a message handler:

Subscriber subscriber = pubSubTemplate.subscribe(subscriptionName, (message) -> {"Message received from " + subscriptionName + " subscription: "
            + message.getPubsubMessage().getData().toStringUtf8());

Some Important methods and their description

Method Signature Method Description
publish(String topic, T payload) Send a message to Pub/Sub. Enviroment
subscribe(String subscription, Consumer messageConsumer) Asynchronously pulls messages and passes them to messageConsumer
subscribeAndConvert(String subscription, Consumer> messageConsumer, Class payloadType)same as pull, but converts message payload to payloadType using the converter configured in the template
pull(String subscription, Integer maxMessages, Boolean returnImmediately)Pulls a number of messages from a subscription, allowing for the retry settings to be configured. Any messages received by pull() are not automatically acknowledged. See Acknowledging messages.The maxMessages parameter is the maximum limit of how many messages to pull from a subscription in a single call; this value must be greater than 0. You may omit this parameter by passing in null; this means there will be no limit on the number of messages pulled (maxMessages will be Integer.MAX_INTEGER).
If returnImmediately is true, the system will respond immediately even if it there are no messages available to return in the Pull response. Otherwise, the system may wait (for a bounded amount of time) until at least one message is available, rather than returning no messages.
pullAndAck(String subscription, Integer maxMessages, Boolean returnImmediately)Works the same as the pull method and, additionally, acknowledges all received messages.
pullNext(String subscription)Allows for a single message to be pulled and automatically acknowledged from a subscription.
pullAndConvert(String subscription, Integer maxMessages, Boolean returnImmediately, Class payloadType)Works the same as the pull method and, additionally, converts the Pub/Sub binary payload to an object of the desired type, using the converter configured in the template.
Note: Above methods are referred from

For More Details please visit the below links:-

For Code Reference