Java idiomatic client for [Google Cloud Pub/Sub] (https://cloud.google.com/pubsub/).
- [Homepage] (https://googlecloudplatform.github.io/google-cloud-java/)
- [API Documentation] (https://googlecloudplatform.github.io/google-cloud-java/apidocs)
Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.
google-cloud-pubsub
uses gRPC as transport layer, which is not (yet) supported by App Engine Standard.google-cloud-pubsub
will work on App Engine Flexible.
Add this to your pom.xml file
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.9.3-alpha</version>
</dependency>
If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:google-cloud-pubsub:0.9.3-alpha'
If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "google-cloud-pubsub" % "0.9.3-alpha"
PubSubExample
is a simple command line interface that provides some of Cloud Pub/Sub's functionality. Read more about using the application on the PubSubExample
docs page.
See the Authentication section in the base directory's README.
Google Cloud Pub/Sub is designed to provide reliable,
many-to-many, asynchronous messaging between applications. Publisher
applications can send messages to a topic
and other applications can
subscribe to that topic to receive the messages. By decoupling senders and
receivers, Google Cloud Pub/Sub allows developers to communicate between
independently written applications.
See the Google Cloud Pub/Sub docs for more details on how to activate Cloud Pub/Sub for your project.
See the google-cloud
API Pub/Sub documentation to learn how to interact with the
Cloud Pub/Sub using this Client Library.
For this tutorial, you will need a
Google Developers Console project with the Pub/Sub API
enabled. You will need to enable billing to
use Google Cloud Pub/Sub.
Follow these instructions to get your
project set up. You will also need to set up the local development environment by installing the
Google Cloud SDK and running the following commands in command line:
gcloud auth login
and gcloud config set project [YOUR PROJECT ID]
.
You'll need to obtain the google-cloud-pubsub
library. See the Quickstart section
to add google-cloud-pubsub
as a dependency in your code.
To make authenticated requests to Google Cloud Pub/Sub, you must create a service object with credentials. You can then make API calls by calling methods on the Pub/Sub service object. The simplest way to authenticate is to use Application Default Credentials. These credentials are automatically inferred from your environment, so you only need the following code to create your service object:
import com.google.cloud.pubsub.PubSub;
import com.google.cloud.pubsub.PubSubOptions;
try(PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) {
// use pubsub here
}
For other authentication options, see the Authentication page.
With Pub/Sub you can create topics. A topic is a named resource to which messages are sent by publishers. Add the following imports at the top of your file:
import com.google.cloud.pubsub.Topic;
import com.google.cloud.pubsub.TopicInfo;
Then, to create the topic, use the following code:
Topic topic = pubsub.create(TopicInfo.of("test-topic"));
With Pub/Sub you can publish messages to a topic. Add the following import at the top of your file:
import com.google.cloud.pubsub.Message;
Then, to publish messages asynchronously, use the following code:
Message message1 = Message.of("First message");
Message message2 = Message.of("Second message");
topic.publishAsync(message1, message2);
With Pub/Sub you can create subscriptions. A subscription represents the stream of messages from a single, specific topic. Add the following imports at the top of your file:
import com.google.cloud.pubsub.Subscription;
import com.google.cloud.pubsub.SubscriptionInfo;
Then, to create the subscription, use the following code:
Subscription subscription =
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
With Pub/Sub you can pull messages from a subscription. Add the following imports at the top of your file:
import com.google.cloud.pubsub.Message;
import com.google.cloud.pubsub.PubSub.MessageConsumer;
import com.google.cloud.pubsub.PubSub.MessageProcessor;
Then, to pull messages asynchronously, use the following code:
MessageProcessor callback = new MessageProcessor() {
@Override
public void process(Message message) throws Exception {
System.out.printf("Received message \"%s\"%n", message.getPayloadAsString());
}
};
// Create a message consumer and pull messages (for 60 seconds)
try (MessageConsumer consumer = subscription.pullAsync(callback)) {
Thread.sleep(60_000);
}
In CreateTopicAndPublishMessages.java and CreateSubscriptionAndPullMessages.java we put together all the code shown above into two programs. The programs assume that you are running on Compute Engine, App Engine Flexible or from your own desktop.
Java 7 or above is required for using this client.
This library has tools to help make tests for code using Cloud Pub/Sub.
See TESTING to read more about testing.
This library follows [Semantic Versioning] (http://semver.org/).
It is currently in major version zero (0.y.z
), which means that anything
may change at any time and the public API should not be considered
stable.
Contributions to this library are always welcome and highly encouraged.
See google-cloud
's CONTRIBUTING documentation and the shared documentation for more information on how to get started.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.
Apache 2.0 - See LICENSE for more information.