Executing code asynchronously

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:

  • The tasks app runs locally as a command line tool to create and add tasks to the queue.
  • The tasks-handler Spring 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:

  1. Download and install the Java SE 11 Development Kit (JDK).
  2. Complete the Cloud Tasks setup instructions.
  3. Download and install Maven to build, deploy, and manage your app.
  4. Initialize the gcloud tool and configure it to use the project you created above.
  5. Install the Cloud SDK app-engine-java component:

    gcloud components install app-engine-java
    

Set up the sample

To download and install the sample:

  1. Clone the sample application repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    Alternatively, you can download the sample as a zip file and extract it.

  2. Navigate to the top level directory for the tasks-handler app:

    cd java-docs-samples/appengine-java11/tasks-handler/

  3. Deploy the tasks-handler app 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.xml file already specifies your project ID, you don't need to include the -Dapp.deploy.projectId property in the command you run.

Creating a queue

Use the Cloud SDK gcloud queues command to create your queue.

  1. At the command line, enter the following:

    gcloud tasks queues create MY_QUEUE

  2. Wait for the queue to initialize. Verify the creation of your queue by using the describe command:

    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:

  1. Move into the appengine-java11/tasks directory and compile the app:

    cd ../tasks
    mvn package

  2. 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 above
    Note: You can find the location ID by using the following gcloud command:
    gcloud tasks locations list

  3. Create a task, targeted at the /tasks/create endpoint 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.

  4. Verify that the task was received by displaying the logs of the worker service:

    gcloud app logs read

What's next

Now that you've completed adding a task to a Cloud Tasks queue, continue exploring Cloud Tasks by looking at the following pages: