Spring Cloud Pub/Sub

Reading Time: 2 minutes

Cloud Pub/Sub

Google Cloud Pub/Sub allows services to communicate asynchronously, with latency on the order of 100 milliseconds.

Pub/Sub is used for streaming analytics and data integration pipelines to ingest and distribute data. It is equally effective as a messaging- oriented middleware for service integration or as a queue to parallelised tasks.

Pub/Sub enables to create systems of event producers and consumers, called publishers and subscribers. Publishers communicate with subscribers asynchronously by broadcasting events, rather than by synchronous remote procedure calls (RPCs).

Springboot with Cloud Pub/Sub

Spring Cloud GCP provides an abstraction layer to publish and subscribe from Google Cloud Pub/Sub topics and to create, list or delete Google Cloud Pub/Sub topics and subscriptions.

A Spring Boot starter is provided to auto-configure the various required Pub/Sub components.

Maven Configuration:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
</dependency>

Gradle Configuration:

dependencies {
    implementation("org.springframework.cloud:spring-cloud-gcp-starter-pubsub")
}

Application Configuration

Properties from the properties file always have precedence over the Spring Boot configuration. The following configuration need to connect GCP pub/sub.

spring.cloud.gcp.project-id= <GCP_PROJECT_ID>
spring.cloud.gcp.credentials.location= <LOCAL_CREDENTIALS_PATH>

Set up Google Cloud Pub/Sub

 Send and receive messages from Google Cloud Pub/Sub, we can create them in the Google Cloud Console or, pragmatically, with the PubSubAdmin class.

GCP Console

Let’s create a topic called “gcpTopicDemo” under project let’s Demo Cloud Pub-Sub

GCP Subscriber Configuration

PubSubTemplate is Spring’s abstraction to subscribe to Google Cloud Pub/Sub topics. The Spring Cloud GCP Pub/Sub Boot starter provides an auto-configured PubSubTemplate instance which can simply inject as a method argument.

@Bean
public PubSubInboundChannelAdapter incommingMessageChannelAdapter(
    @Qualifier("GCPInputChannel") MessageChannel inputChannel,
    PubSubTemplate pubSubTemplate) {
  PubSubInboundChannelAdapter adapter =
    new PubSubInboundChannelAdapter(pubSubTemplate, "gcpTopicDemo");
  adapter.setOutputChannel(inputChannel);
  return adapter;
}

This service to process incoming messages.

@Bean
@ServiceActivator(inputChannel = "GCPInputChannel")
public MessageHandler messageReceiver() {
  return message -> {
    LOGGER.info("Message received: " + new String((byte[])  message.getPayload()));
    BasicAcknowledgeablePubsubMessageoriginalMessage=message.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE,BasicAcknowledgeablePubsubMessage.class);
  };
  }

GCP Publisher Configuration

PubSubTemplate is Spring’s abstraction to publish messages to Google Cloud Pub/Sub topics. The Spring Cloud GCP Pub/Sub Boot starter provides an auto-configured PubSubTemplate instance.

@Bean
@ServiceActivator(inputChannel = "GCPOutputChannel")
public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
  return new PubSubMessageHandler(pubsubTemplate, "gcpTopicDemo");
}

This method publisher the message into the topic

@Autowired
private PubsubOutboundGateway messagingGateway;

public void publishMessage(@RequestParam("message") String message) {
  messagingGateway.sendToPubsub(message);
}

Create a jar and executable

We can run the application from the command line with Gradle or Maven or build a executable JAR file that contains all the necessary dependencies, classes, and resources and run that.

>> java -jar spring-gcp-pubsub.jar

In this blog i have covered basic understanding of GCP pub/sub, created topic from GCP console and integration with spring-boot with configuration.

Written by 

Abid Khan is a Lead Consultant at Knoldus Inc., postgraduate (MCA), and having 5+ years of experience in JavaSE, JavaEE, ORM framework, Spring, Spring-boot, RESTful Web Services, Kafka, MQTT, Rabbitmq, Docker, Redis, MySQL, Maven, GIT, etc. He is a well-developed professional with a prolific track record of designing, testing, and monitoring software as well as upgrading the existing programs.

Leave a Reply