Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Using Pull Queues in Java

Pull queues allow you to design your own system to consume App Engine tasks. The task consumer can be part of your App Engine app (such as a backend ) or a system outside of App Engine (using the Task Queue REST API ). The task consumer leases a specific number of tasks for a specific duration, then processes and deletes them before the lease ends.

Using pull queues requires your application to handle some functions that are automated in push queues:

  • Your application needs to scale the number of workers based on processing volume. If your application does not handle scaling, you risk wasting computing resources if there are no tasks to process; you also risk latency if you have too many tasks to process.
  • Your application also needs to explicitly delete tasks after processing. In push queues, App Engine deletes the tasks for you. If your application does not delete pull queue tasks after processing, another worker might re-process the task. This wastes computing resources and risks errors if tasks are not idempotent .

Pull queues require a specific configuration in queue.xml . For more information, please see Defining Pull Queues on the Task Queue configuration page.

The following sections describe the process of enqueuing, leasing, and deleting tasks using pull queues.

  1. Pull queue overview
  2. Pulling tasks within App Engine
  3. Pulling tasks to a backend
  4. Pulling tasks from outside App Engine
  5. Quotas and limits for pull queues

Pull queue overview

Pull queues allow a task consumer to process tasks outside of App Engine's default task processing system. If the task consumer is a part of your App Engine app, you can manipulate tasks using simple API calls from the com.google.appengine.api.taskqueue package. Task consumers outside of App Engine can pull tasks using the Task Queue REST API .

The process works like this:

  1. The task consumer leases tasks, either via the Task Queue API (if the consumer is internal to App Engine) or the Task Queue REST API (if the consumer is external to App Engine).
  2. App Engine sends task data to the consumer.
  3. The consumer processes the tasks. If the task fails to execute before the lease expires, the consumer can lease it again. This counts as a retry attempt, and you can configure the maximum number of retry attempts before the system deletes the task.
  4. Once a task executes successfully, the task consumer must delete it.
  5. The task consumer is responsible for scaling instances based on processing volume.

Pulling tasks within App Engine

You can use pull queues within the App Engine environment using simple API calls to add tasks to a pull queue, lease them, and delete them after processing.

Before you begin, make sure to configure the pull queue in queue.xml .

Adding tasks to a pull queue

To add tasks to a pull queue, simply get the queue using the queue name defined in queue.xml , and use TaskOptions.Method.PULL . The following example enqueues tasks in a pull queue named pull-queue :

Queue q = QueueFactory.getQueue("pull-queue");
q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
                                     .payload("hello world"));

Leasing tasks

Once you have added tasks to a pull queue, you can lease one or more tasks using leaseTasks() . There may be a short delay before tasks recently added using add() become available via leaseTasks() . When you request a lease, you specify the number of tasks to lease (up to a maximum of 1,000 tasks) and the duration of the lease in seconds (up to a maximum of one week). The lease duration needs to be long enough to ensure that the slowest task will have time to finish before the lease period expires. You can extend a task lease using modifyTaskLease() .

Leasing a task makes it unavailable for processing by another worker, and it remains unavailable until the lease expires. If you lease an individual task, the API selects the task from the front of the queue. If no such task is available, an empty list is returned.

Queue q = QueueFactory.getQueue("pull-queue");

List<TaskHandle> tasks = q.leaseTasks(3600, TimeUnit.SECONDS, 100);

Experimental!

Task Tagging is an experimental, innovative, and rapidly changing new feature for Google App Engine. Unfortunately, being on the bleeding edge means that we may make backwards-incompatible changes to Task Tagging. We will inform the community when this feature is no longer experimental.


Not all tasks are alike; your code can "tag" tasks and then choose tasks to lease by tag. The tag acts as a filter.

Queue q = QueueFactory.getQueue("pull-queue");

q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
                                     .payload("parse1")
                                     .tag("parse".getBytes()));
q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
                                     .payload("parse2")
                                     .tag("parse".getBytes()));
q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
                                     .payload("render1")
                                     .tag("render".getBytes()));
q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
                                     .payload("render2")
                                     .tag("render".getBytes()));

// Lease render tasks, but not parse
q.leaseTasksByTag(3600, TimeUnit.SECONDS, 100, "render");

// You can also specify a tag to lease via LeaseOptions passed to leaseTasks.

Deleting tasks

In general, once a worker completes a task, it needs to delete the task from the queue. If you see tasks remaining in a queue after a worker finishes processing them, it is likely that the worker failed; in this case, the tasks need to be processed by another worker.

You can delete an individual task or a list of tasks using deleteTask() . You must know the name of a task in order to delete it. If you are deleting tasks from a pull queue, you can find task names in the Task object returned by leaseTasks() . If you are deleting tasks from a push queue, you need to know the task name through some other means (for example, if you created the task explicitly).

The following code sample demonstrates how to deletes a task named foo from the queue named pull-queue :

Queue q = QueueFactory.getQueue("pull-queue");
q.deleteTask("foo");

Pulling tasks to a backend

You can use App Engine Backends as workers to lease and process pull queue tasks. Backends allow you to process more work without having to worry about request deadlines and other restrictions normally imposed by App Engine. Using backends with pull queues gives you processing efficiencies by allowing you to batch task processing using leases.

For more information about using backends, check out the Backends documentation .

Pulling tasks from outside App Engine

If you need to use pull queues from outside App Engine, you must use the Task Queue REST API . The REST API is a Google web service accessible at a globally-unique URI of the form:

https://www.googleapis.com/taskqueue/v1beta2/projects/taskqueues

Google provides the following client libraries that you can use to call the Task Queue methods remotely:

In the tables below, the first column shows each library's stage of development (note that some are in early stages), and links to documentation for the library. The second column links to available samples for each library.

Documentation Samples
Google APIs Client Library for Java (rc) Java samples
Google APIs Client Library for JavaScript (beta) JavaScript samples
Google APIs Client Library for .NET .NET samples
Google APIs Client Library for Objective-C Objective-C samples
Google APIs Client Library for PHP (beta) PHP samples
Google APIs Client Library for Python Python samples

These early-stage libraries are also available:

Documentation Samples
Google APIs Client Library for Go (alpha) Go samples
Google APIs Client Library for Node.js (alpha) Node.js samples
Google APIs Client Library for Ruby (alpha) Ruby samples

In addition to the Google-supported client libraries listed in the table above, a set of third-party Dart libraries (with samples ) is available for the Dart language .

Prerequisites

The REST API uses OAuth as the authorization mechanism. When you configure your pull queue, make sure that your queue.xml file supplies the email addresses of the users that can access the queue using the REST API. The OAuth scope for all methods is https://www.googleapis.com/auth/taskqueue .

Using the task queue REST API with the Java Google API library

This section demonstrates the use of the REST API in an application called gtaskqueue . The gtaskqueue tool allows you to interact with the REST API via the command line and also provides an example of how to lease and delete tasks. The sections below show the basics of using the Client Library with relevant code snippets from the gtaskqueue tool.

Importing the Client Library for Java

Most of the functionality of the Client Library is encapsulated in an HttpTransport object. This object stores your authentication header and the default headers to be used with every request. Once the authentication is complete (see Auth.java in the sample code for how OAuth is handled), we set up an HttpTransport instance that will be used for all requests. From Util.java :

import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.GoogleUtils;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
...
public static final HttpTransport TRANSPORT = newTransport();
...
static HttpTransport newTransport() {
    HttpTransport result = new NetHttpTransport();
    GoogleUtils.useMethodOverride(result);
    GoogleHeaders headers = new GoogleHeaders();
    headers.setApplicationName("Google-TaskQueueSample/1.0");
    result.defaultHeaders = headers;
    return result;
}
...

The HttpTransport instance can now be used to make requests to the REST API. For example, here is the code from TaskQueue.java that calls TaskQueue.Get and displays information about the queue:

public void get(String projectName, String taskQueueName, boolean getStats) throws IOException {
  HttpRequest request = Util.TRANSPORT.buildGetRequest();
  request.url = TaskQueueUrl.forTaskQueueServiceQueues(projectName,
                                                       taskQueueName,
                                                       getStats);
  return request.execute().parseAs(TaskQueue.class);
}

The following sections describe the two most common functions used with the Task Queue API, allowing you to lease and delete tasks.

Leasing Tasks

The Java library provides methods that invoke the Tasks.lease method in the REST API. When you create a lease, you need to specify the number of tasks to lease (up to a maximum of 1,000 tasks); the API returns the specified number of tasks in order of the oldest task ETA.

You also need to specify the duration of the lease in seconds (up to a maximum of one week). The lease must be long enough so you can finish all the leased tasks, yet short enough so that tasks can be made available for leasing if your consumer crashes. Similarly, if you lease too many tasks at once and your client crashes, a large number of tasks will become unavailable until the lease expires.

The following code from the gtaskqueue sample (in TaskQueueSample.java ) shows how to lease tasks:

private static void leaseAndDeleteTask() throws IOException {
  Task task = new Task();
  List<Task> leasedTasks = task.lease(projectName, taskQueueName, leaseSecs,
      numTasks);
  if (leasedTasks.size() == 0) {
    System.out.println("No tasks to lease and hence exiting");
  }
  for (Task leasedTask : leasedTasks) {
    leasedTask.executeTask();
    leasedTask.delete();
  }
}

This code enables the command-line tool to lease a specified number of tasks for a set duration:

mvn -q exec:java -Dexec.args="appengtaskpuller 30 100"

When run, this command-line tool constructs the following URI call to the REST API:

POST
https://www.googleapis.com/taskqueue/v1beta2/projects/taskqueues/appengtaskpuller/tasks/lease?alt=json&lease_secs=30&numTasks=100

This request returns an array of 100 tasks with the following JSON structure:

{
  "kind": "taskqueues#tasks",
  "items": [
    {
      "kind": "taskqueues#task",
      "id": string,
      "queueName": string,
      "payloadBase64": string,
      "enqueueTimestamp": number,
      "leaseTimestamp": number
    }
    ...
  ]
}

After processing each task, you need to delete it, as described in the following section.

Deleting Tasks

In general, once a worker completes a task, it needs to delete the task from the queue. If you see tasks remaining in a queue after a worker finishes processing, it is likely that the worker failed; in this case, the tasks need to be processed by another worker.

You can delete an individual task or a list of tasks using the REST method Task.delete . You must know the name of a task in order to delete it. You can get the task name from the id field of the Task object returned by Task.lease .

Call delete if you have finished a task, even if you have exceeded the lease time. Tasks should be idempotent , so even if a task lease expires and another client leases the task, performing the same task twice should not cause an error.

Returning to our gtaskqueue example, the following code snippet (from TaskQueueSample.java ) deletes tasks from a queue:

private static void leaseAndDeleteTask() throws IOException {
  Task task = new Task();
  List<Task> leasedTasks = task.lease(projectName, taskQueueName, leaseSecs,
      numTasks);
  if (leasedTasks.size() == 0) {
    System.out.println("No tasks to lease and hence exiting");
  }
  for (Task leasedTask : leasedTasks) {
    leasedTask.executeTask();
    leasedTask.delete();
  }
}

The delete method implementation can be seen in Task.java :

public void delete() throws HttpResponseException, IOException {
  HttpRequest request = Util.TRANSPORT.buildDeleteRequest();
  String projectName = getProjectFromQueueName();
  String queueName = getQueueFromQueueName();
  if (projectName.isEmpty() || queueName.isEmpty()) {
    System.out.println("Error parsing full queue name:" + this.queueName +
        " Hence unable to delete task" + this.id);
    return;
  }
  request.url = TaskQueueUrl.forTaskQueueServiceTasks(projectName, queueName);
  request.url.pathParts.add(this.id);
  try {
    request.execute();
  } catch (HttpResponseException hre) {
    System.out.println("Error deleting task: " + this.id);
    throw hre;
  }
}

If the delete command is successful, the REST API returns an HTTP 200 response. If deletion fails, the API returns an HTTP failure code.

Quotas and limits for pull queues

Enqueuing a task counts counts toward the following quotas:

  • Task Queue Stored Task Count
  • Task Queue API Calls
  • Task Queue Stored Task Bytes

Leasing a task counts toward the following quotas:

  • Task Queue API Calls
  • Outgoing Bandwidth (if using the REST API)

The Task Queue Stored Task Bytes quota is configurable in queue.xml by setting <total-storage-limit> . This quota counts towards your Stored Data (billable) quota.

The following limits apply to the use of pull queues:

Pull Queue Limits
Maximum task size 1MB
Maximum number of active queues (not including the default queue) Free apps: 10 queues, Billed apps: 100 queues
Maximum countdown/ETA for a task 30 days from the current date and time
Maximum number of tasks that can be added in a batch 100 tasks
Maximum number of tasks that can be added in a transaction 5 tasks
Maximum number of tasks that you can lease in a single operation 1000 tasks
Maximum payload size when leasing a batch of tasks 32MB (1MB when using the REST API)

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.