
Inroduction to Pub/Sub
Google Cloud Pub/Sub is a messaging service that enables you to build scalable, event-driven applications on Google Cloud Platform. It follows the publish-subscribe pattern, allowing various components of your applications to communicate asynchronously. In this blog post, we’ll walk you through creating a Pub/Sub topic and subscription using Java code.
Key features of Pub/Sub
- Scalability: Google Cloud Pub/Sub is designed in such a way that it can handle massive amounts of data. Moreover it can scale to meet your application’s needs.
- Durability: Messages are stored in Pub/Sub topics additionally ensures that no message is lost, even in the face of failures.
- Real-time: Pub/Sub allows for real-time communication between different parts of your application, in addition it suitable for building event-driven systems.
- Integration: It seamlessly integrates with other GCP services like Cloud Functions, Dataflow, and more, thus making it a powerful tool for building cloud-native applications.
Core concept of Pub/Sub
- Topic: To which publisher sends the messages.
- Subscription: In order to publish data to the topic, we must subscribe to that topic. The subscription represents the streaming message from a unique topic and delivered to the subscribing application.
- Message: It is the data that is going to be published and subscribed.
- Message attribute: The publisher can define a key-value pair for the message, Depending on the receiver’s language
Setting Up Your Java Project
For this example, we’ll use the Java client library for Google Cloud pub sub. Add the dependency to your project’s pom.xml
if you’re using Maven:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.114.6</version> <!-- Use the latest version -->
</dependency>
Creating a Topic
A topic is a named resource where the messages can be sent. Let’s create a topic programmatically using Java:
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.pubsub.v1.ProjectTopicName;
public class CreatePubSubTopic {
public static void main(String[] args) {
String projectId = "your-project-id";
String topicId = "your-topic-id";
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
topicAdminClient.createTopic(topicName);
System.out.println("Topic created: " + topicName.toString());
} catch (ApiException e) {
System.err.println("Topic creation failed: " + e.getMessage());
}
}
}
Replace "your-project-id"
and "your-topic-id"
with your GCP project ID and the desired topic ID. This code creates a Pub/Sub topic in your project.
Creating a Subscription
The subscription in the Pub/Sub represents the stream of messages from a single, specific pub sub topic. This message is delivered to subscribing application. Let’s create a subscription programmatically using Java:
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.TopicName;
public class CreatePubSubSubscription {
public static void main(String[] args) {
String projectId = "your-project-id";
String topicId = "your-topic-id";
String subscriptionId = "your-subscription-id";
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
TopicName topicName = TopicName.of(projectId, topicId);
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.createSubscription(
subscriptionName,
topicName,
com.google.pubsub.v1.PushConfig.getDefaultInstance(),
10);
System.out.println("Subscription created: " + subscriptionName.toString());
} catch (ApiException e) {
System.err.println("Subscription creation failed: " + e.getMessage());
}
}
}
Replace "your-project-id"
, "your-topic-id"
, and "your-subscription-id"
with your GCP project ID, topic ID, and the desired subscription ID. This code creates a Pub/Sub subscription that subscribes to the specified topic.
Conclusion
In this blog post, you’ve learned how to create a pub sub topic and subscription in Google Cloud using Java. These foundational steps enable you to build event-driven and scalable applications on the Google Cloud Platform. Hence allows you to decouple components, process messages asynchronously, and handle large volumes of data with ease.
Reference link :- https://cloud.google.com/pubsub/docs/overview