Queue tasks for asynchronous code execution using the Cloud Tasks REST API, RPC API, or the Google Cloud Client library and a Java 11 App Engine standard service as a push target.
This example uses two applications:
tasksapp runs locally as a command line tool to create and add tasks to the queue.
tasks-handlerSpring Boot app processes Cloud Tasks requests as a worker by serving as an endpoint to receive task requests. This is the main App Engine app you deploy.
Before you begin
To deploy the sample app:
- Download and install the Java SE 11 Development Kit (JDK).
- Complete the Cloud Tasks setup instructions.
- Download and install Maven to build, deploy, and manage your app.
- Initialize the
gcloudtool and configure it to use the project you created above.
Install the Cloud SDK
gcloud components install app-engine-java
Set up the sample
To download and install the sample:
Clone the sample application repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.gitAlternatively, you can download the sample as a zip file and extract it.
Navigate to the top level directory for the
tasks-handlerapp to App Engine:
mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID
Replace PROJECT_ID with the ID of your Cloud project. If your
pom.xmlfile already specifies your project ID, you don't need to include the
-Dapp.deploy.projectIdproperty in the command you run.
Creating a queue
Use the Cloud SDK
gcloud queues command to create your queue.
At the command line, enter the following:
gcloud tasks queues create MY_QUEUE
Wait for the queue to initialize. Verify the creation of your queue by using the
gcloud tasks queues describe MY_QUEUE
The output should look similar to this:
name: projects/PROJECT_ID/locations/LOCATION_ID/queues/MY_QUEUE // Note these ids rateLimits: maxBurstSize: 100 maxConcurrentDispatches: 1000 maxDispatchesPerSecond: 500.0 retryConfig: maxAttempts: 100 maxBackoff: 3600s maxDoublings: 16 minBackoff: 0.100s state: RUNNING
Adding a task to the queue
Create a task locally, add it to the queue you set up, and deliver the task to an asynchronous worker:
Move into the
appengine-java11/tasksdirectory and compile the app:
cd ../tasks mvn package
Set the following environment variables on your machine. The sample app uses these to create the request that adds tasks to your queue:
export GOOGLE_CLOUD_PROJECT=PROJECT_ID // The project ID you set up above export LOCATION_ID=LOCATION_ID // The region in which your queue is running export QUEUE_ID=MY_QUEUE // The queue you created aboveNote: You can find the location ID by using the following
gcloud tasks locations list
Create a task, targeted at the
/tasks/createendpoint on the task handler controller you deployed, with a specified payload. The payload can be any data from the request that the worker needs to complete processing the task; in this example, it's hard-coded into the sample and doesn't need to be specified:
mvn exec:java -Dexec.mainClass="com.example.task.CreateTask"Once the task gets passed to the worker and the worker processes the task, the worker will return a 2xx success status code to the Cloud Tasks service and delete the task from the queue automatically.
Verify that the task was received by displaying the logs of the worker service:
gcloud app logs read
Now that you've completed adding a task to a Cloud Tasks queue, continue exploring Cloud Tasks by looking at the following pages:
- Manage existing tasks and queues, including deleting them with Managing Queues and Tasks.
- Create and configure Cloud Tasks queues with the Creating Cloud Tasks Queues guide.
- Learn more about queue management using gRPC in the gRPC API reference.
- Learn more about queue management using REST in the REST API reference.
- Find out more about Cloud Tasks queues with Cloud Tasks Overview.