Créer une file d'attente

Crée une file d'attente.

Exemple de code

Java

Pour savoir comment installer et utiliser la bibliothèque cliente pour Cloud Tasks, consultez la page Bibliothèques clientes Cloud Tasks.

import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.LocationName;
import com.google.cloud.tasks.v2.Queue;
import com.google.cloud.tasks.v2.QueueName;
import java.io.IOException;

public class CreateQueue {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String locationId = "us-central1";
    String queueId = "my-queue";
    createQueue(projectId, locationId, queueId);
  }

  // Create a queue using the Cloud Tasks client.
  public static void createQueue(String projectId, String locationId, String queueId)
      throws IOException {

    // Instantiates a client.
    try (CloudTasksClient client = CloudTasksClient.create()) {

      // Construct the fully qualified location.
      String parent = LocationName.of(projectId, locationId).toString();

      // Construct the fully qualified queue path.
      String queuePath = QueueName.of(projectId, locationId, queueId).toString();

      // Send create queue request.
      Queue queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build());

      System.out.println("Queue created: " + queue.getName());
    }
  }
}

Node.js

Pour savoir comment installer et utiliser la bibliothèque cliente pour Cloud Tasks, consultez la page Bibliothèques clientes Cloud Tasks.

// Imports the Google Cloud Tasks library.
const cloudTasks = require('@google-cloud/tasks');

// Instantiates a client.
const client = new cloudTasks.CloudTasksClient();

async function createQueue() {
  // Send create queue request.
  const [response] = await client.createQueue({
    // The fully qualified path to the location where the queue is created
    parent: client.locationPath(project, location),
    queue: {
      // The fully qualified path to the queue
      name: client.queuePath(project, location, queue),
      appEngineHttpQueue: {
        appEngineRoutingOverride: {
          // The App Engine service that will receive the tasks.
          service: 'default',
        },
      },
    },
  });
  console.log(`Created queue ${response.name}`);
}
createQueue();

Python

Pour savoir comment installer et utiliser la bibliothèque cliente pour Cloud Tasks, consultez la page Bibliothèques clientes Cloud Tasks.

def create_queue(project, queue_name, location):
    """Create a task queue."""

    from google.cloud import tasks_v2

    # Create a client.
    client = tasks_v2.CloudTasksClient()

    # Construct the fully qualified location path.
    parent = f"projects/{project}/locations/{location}"

    # Construct the create queue request.
    queue = {"name": client.queue_path(project, location, queue_name)}

    # Use the client to create the queue.
    response = client.create_queue(request={"parent": parent, "queue": queue})

    print("Created queue {}".format(response.name))
    return response