Google Cloud Pub/Sub is a fully-managed real-time messaging service that allows you to send and receive messages between independent applications. This sample Java application demonstrates how to access the Pub/Sub API using the Google Cloud Client Library for Java.
For more samples, see the samples in google-cloud-java.
- Install Maven
- Install the Google Cloud SDK and authenticate :
gcloud config set project [YOUR PROJECT ID]
gcloud auth application-default login
- Enable Pub/Sub API
- Build your project with:
mvn clean package -DskipTests
mvn exec:java -Dexec.mainClass=com.example.pubsub.CreateTopicExample -Dexec.args=my-topic-id
mvn exec:java -Dexec.mainClass=com.example.pubsub.CreatePullSubscriptionExample -Dexec.args="my-topic-id my-subscription-id"
mvn exec:java -Dexec.mainClass=com.example.pubsub.PublisherExample -Dexec.args=my-topic-id
Publishes 5 messages to the topic my-topic-id.
mvn exec:java -Dexec.mainClass=com.example.pubsub.SubscriberExample -Dexec.args=my-subscription-id
Subscriber will continue to listen on the topic for 5 minutes and print out message id and data as messages are received.
Run the test with Maven.
mvn clean -Dtest=com.example.pubsub.QuickStartIT verify