Google Cloud Pub/Sub Java Client Library
repository·main·Indexed 41 days ago
https://github.com/googleapis/java-pubsubA Java idiomatic client library for Google Cloud Pub/Sub. Provides tools to create topics via TopicAdminClient, manage subscriptions via SubscriptionAdminClient, publish messages asynchronously using the Publisher class, and consume messages using the Subscriber class. Requires Java 8 or above, with Java 11 and 17 recommended.
What's inside Google Cloud Pub/Sub
- Cloud Pub/Sub utilizes both gRPC and HTTP/JSON for its transport layer.
Explore Google Cloud Pub/Sub Java Samples
mainThe repository contains a comprehensive collection of Java code samples located in thesamples/directory. These samples demonstrate various Pub/Sub operations, including schema management, subscription types, topic ingestion from external services, and advanced publishing/subscribing configurations. Many samples can be opened directly in Google Cloud Shell for immediate testing.Run Pub/Sub samples integration tests
mainYou can run the integration tests for the Pub/Sub samples using Maven by targeting the specific test class
pubsub.AdminIT:mvn -Dtest=pubsub.AdminIT testRun Pub/Sub Java samples
mainTo run the provided code samples, follow these steps:
Clone the repository and navigate to the
snippetsdirectory:git clone https://github.com/googleapis/java-pubsub cd java-pubsub/samples/snippetsAuthenticate with Google Cloud: Use Application Default Credentials (ADC) to provide access to your GCP resources:
gcloud auth application-default loginExecute a specific sample: Use Maven to run a sample class (ensure the class has a completed 'to-do' section). For example, to run
CreateTopicExample:mvn clean compile exec:java -Dexec.mainClass=pubsub.CreateTopicExample
git clone https://github.com/googleapis/java-pubsub cd java-pubsub/samples/snippets gcloud auth application-default login mvn clean compile exec:java -Dexec.mainClass=pubsub.CreateTopicExampleSet up the local development environment for Pub/Sub
mainBefore using the client library, ensure you have a Google Cloud project with the Cloud Pub/Sub API enabled and billing active. To authenticate your local environment, install the Google Cloud CLI and run:
gcloud auth loginto authenticate your account.gcloud config set project [YOUR PROJECT ID]to set your active project.
gcloud auth login gcloud config set project [YOUR PROJECT ID]Check supported Java versions
mainThe Cloud Pub/Sub client requires Java 8 or above.
For new development, Java 11 and Java 17 are recommended.
Google tests its client libraries against all current LTS versions covered by Oracle's Extended Support. While legacy support is provided for older runtimes (like Java 7) on a best-effort basis, it is recommended to upgrade to a version supported by the current library to ensure full feature availability and patches.
Install the Google Cloud Pub/Sub client for Java
mainAdd the
google-cloud-pubsublibrary to your project using Maven, Gradle, or SBT. It is recommended to use the Google Cloud Libraries BOM (Bill of Materials) to manage dependency versions consistently.### Maven with BOM ```xml <dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>libraries-bom</artifactId> <version>26.76.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-pubsub</artifactId> </dependency> </dependencies>Maven without BOM
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-pubsub</artifactId> <version>1.148.0</version> </dependency>Gradle 5.x+ with BOM
implementation platform('com.google.cloud:libraries-bom:26.80.0') implementation 'com.google.cloud:google-cloud-pubsub'Gradle without BOM
implementation 'com.google.cloud:google-cloud-pubsub:1.150.2'SBT
libraryDependencies += "com.google.cloud" % "google-cloud-pubsub" % "1.150.2"Troubleshoot Cloud Pub/Sub for Java
mainIf you encounter issues while using the Cloud Pub/Sub client library, refer to the shared troubleshooting documentation maintained by the Google Cloud Java client libraries team.Publish messages to a topic asynchronously
mainUse the
Publisherclass to send messages to a topic. Publishing is asynchronous and returns anApiFuture<String>which contains the message ID upon success.Required imports:
com.google.api.core.ApiFuturecom.google.api.core.ApiFutureCallbackcom.google.api.core.ApiFuturescom.google.cloud.pubsub.v1.Publishercom.google.common.util.concurrent.MoreExecutorscom.google.protobuf.ByteStringcom.google.pubsub.v1.PubsubMessage
Publisher publisher = null; try { publisher = Publisher.newBuilder(topic).build(); ByteString data = ByteString.copyFromUtf8("my-message"); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage); ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() { public void onSuccess(String messageId) { System.out.println("published with message id: " + messageId); } public void onFailure(Throwable t) { System.out.println("failed to publish: " + t); } }, MoreExecutors.directExecutor()); //... } finally { if (publisher != null) { publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); } }Pull messages from a subscription asynchronously
mainUse the
Subscriberclass to pull messages from a subscription. You must implement aMessageReceiverto handle incoming messages and callconsumer.ack()to acknowledge receipt.Required imports:
com.google.cloud.pubsub.v1.AckReplyConsumercom.google.cloud.pubsub.v1.MessageReceivercom.google.cloud.pubsub.v1.Subscribercom.google.common.util.concurrent.MoreExecutorscom.google.pubsub.v1.PubsubMessagecom.google.pubsub.v1.SubscriptionName
import com.google.cloud.pubsub.v1.AckReplyConsumer; import com.google.cloud.pubsub.v1.MessageReceiver; import com.google.cloud.pubsub.v1.Subscriber; import com.google.common.util.concurrent.MoreExecutors; import com.google.pubsub.v1.PubsubMessage; import com.google.pubsub.v1.SubscriptionName; SubscriptionName subscription = SubscriptionName.of("test-project", "test-subscription"); MessageReceiver receiver = new MessageReceiver() { @Override public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { System.out.println("got message: " + message.getData().toStringUtf8()); consumer.ack(); } }; Subscriber subscriber = null; try { subscriber = Subscriber.newBuilder(subscription.toString(), receiver).build(); subscriber.addListener( new Subscriber.Listener() { @Override public void failed(Subscriber.State from, Throwable failure) { System.err.println(failure); } }, MoreExecutors.directExecutor()); subscriber.startAsync().awaitRunning(); //... } finally { if (subscriber != null) { subscriber.stopAsync(); } }Create a Pub/Sub subscription
mainUse
SubscriptionAdminClientto create a subscription. A subscription represents a stream of messages from a specific topic.Required imports:
com.google.cloud.pubsub.v1.SubscriptionAdminClientcom.google.cloud.pubsub.v1.PushConfigcom.google.pubsub.v1.SubscriptionNamecom.google.pubsub.v1.TopicName
import com.google.cloud.pubsub.v1.SubscriptionAdminClient; import com.google.cloud.pubsub.v1.PushConfig; import com.google.pubsub.v1.SubscriptionName; import com.google.pubsub.v1.TopicName; TopicName topic = TopicName.of("test-project", "test-topic"); SubscriptionName subscription = SubscriptionName.of("test-project", "test-subscription"); try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0); }Create a Pub/Sub topic
mainUse
TopicAdminClientto create a new topic. A topic is a named resource to which messages are sent by publishers.Required imports:
com.google.cloud.pubsub.v1.TopicAdminClientcom.google.pubsub.v1.TopicName
import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.pubsub.v1.TopicName; TopicName topic = TopicName.of("test-project", "test-topic"); try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { topicAdminClient.createTopic(topic); }