큐 만들기

큐를 만듭니다.

더 살펴보기

이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.

코드 샘플

Java

Cloud Tasks용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud Tasks 클라이언트 라이브러리를 참조하세요.

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

Cloud Tasks용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud Tasks 클라이언트 라이브러리를 참조하세요.

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

Cloud Tasks용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud Tasks 클라이언트 라이브러리를 참조하세요.

Cloud Tasks에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

from google.cloud import tasks_v2

def create_queue(project: str, location: str, queue_id: str) -> tasks_v2.Queue:
    """Create a queue.
    Args:
        project: The project ID to create the queue in.
        location: The location to create the queue in.
        queue_id: The ID to use for the new queue.

    Returns:
        The newly created queue.
    """

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

    # Use the client to send a CreateQueueRequest.
    return client.create_queue(
        tasks_v2.CreateQueueRequest(
            parent=client.common_location_path(project, location),
            queue=tasks_v2.Queue(name=client.queue_path(project, location, queue_id)),
        )
    )

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.