In this article I will show you how to publish and receive messages in PubSub with Java
create topic
1
gcloud pubsub topics create my-topic
create subscription to this topic
1
gcloud pubsub subscriptions create my-sub --topic my-topic
git clone project into cloud shell
1
git clone https://github.com/googleapis/java-pubsub.git
go into the sample
1
cd samples/snippets/
modify PublisherExample.java and SubscribeAsyncExample.java to put the right project id, topic id and subscription id
compile project
1
mvn clean install -DskipTests
run subscriber
1
mvn exec:java -Dexec.mainClass="pubsub.SubscribeAsyncExample"
run publisher in another screen and observe subscriber
1
mvn exec:java -Dexec.mainClass="pubsub.PublisherExample"