Region ID
The REGION_ID
is an abbreviated code that Google assigns
based on the region you select when you create your app. The code does not
correspond to a country or province, even though some region IDs may appear
similar to commonly used country and province codes. For apps created after
February 2020, REGION_ID.r
is included in
App Engine URLs. For existing apps created before this date, the
region ID is optional in the URL.
Learn more about region IDs.
Pub/Sub provides 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.
This document describes how to use the Cloud Client Library to send and receive Pub/Sub messages in a Java 8 app.
Prerequisites
- Follow the instructions in "Hello, World!" for Java 8 on App Engine to set up your environment and project, and to understand how App Engine Java 8 apps are structured.
- Write down and save your project ID, because you will need it to run the sample application described in this document.
Cloning the sample app
Copy the sample apps to your local machine, and navigate to the pubsub
directory:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/appengine-java8/pubsub
Creating a topic and subscription
Create a topic and subscription, which includes specifying the endpoint to which the Pub/Sub server should send requests:
bv # Configure the topic gcloud pubsub topics create YOUR_TOPIC_NAME # Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10
Replace YOUR_TOKEN
with a secret random token. The push endpoint uses this to verify requests.
To use Pub/Sub with authentication, create another subscription:
# Configure the push subscription gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \ --topic=YOUR_TOPIC_NAME \ --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\ --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\ --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \ --ack-deadline=10 # Your service agent # `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the # `iam.serviceAccountTokenCreator` role. PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\ --role='roles/iam.serviceAccountTokenCreator'
mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID
Replace PROJECT_ID with the ID of your Google Cloud project. If
your pom.xml
file already
specifies your
project ID, you don't need to include the -Dapp.deploy.projectId
property in the
command you run.
You can now access the application at
https://PROJECT_ID.REGION_ID.r.appspot.com
.
You can use the form to submit messages, but there's no guarantee of which
instance of your application will receive the notification. You can send
multiple messages and refresh the page to see the received message.