Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Anda dapat membuat tugas HTTP menggunakan metode BufferTask atau
metode CreateTask. Halaman ini memberikan panduan tentang cara memilih antara
dua metode untuk membuat tugas HTTP.
Metode BufferTask memungkinkan Anda membuat tugas dengan mengirim permintaan HTTP ke antrean. Metode CreateTask memungkinkan Anda membuat tugas dengan membuat objek
tugas. Untuk sebagian besar kasus penggunaan, membuat objek tugas secara eksplisit
tidak diperlukan. Metode terbaik untuk membuat tugas bergantung pada apakah antrean Anda
memiliki pemilihan rute tingkat antrean.
Sebelum memulai
Periksa daftar kasus penggunaan berikut untuk melihat apakah ada yang berlaku untuk Anda:
Anda ingin membuat tugas menggunakan RPC API atau library klien (bukan RPC API)
Anda ingin membuat tugas sebagai objek tugas App Engine (bukan sebagai objek tugas yang lebih umum untuk target HTTP generik yang mungkin atau mungkin bukan target App Engine)
Jika salah satu hal ini berlaku untuk Anda, Anda tidak perlu memilih metode pembuatan tugas karena hanya satu metode (metode CreateTask) yang didukung. Buka langsung dokumentasi CreateTask untuk jenis tugas Anda:
LOCATION: region
tempat antrean Anda berada. Jika tidak ditentukan, dan jika ada aplikasi terkait, lokasi aplikasi App Engine project Anda akan digunakan.
Pada output, cari kolom httpTarget dan periksa apakah
uriOverride telah ditetapkan.
Jika output menyertakan baris untuk uriOverride dengan host yang ditentukan,
antrean Anda memiliki pemilihan rute tingkat antrean. Untuk memilih metode pembuatan tugas, lihat
bagian Antrean dengan pemilihan rute tingkat antrean.
Untuk antrean dengan perutean tingkat antrean, metode pembuatan tugas bergantung pada
persyaratan perutean tugas:
Tugas memiliki persyaratan pemilihan rute yang sama seperti yang ditentukan di tingkat antrean:
Gunakan BufferTask. Pelajari cara membuat tugas dengan metode
BufferTask.
Tugas memiliki persyaratan pemilihan rute yang berbeda dari yang ditentukan di tingkat antrean:
Tetapkan
UriOverrideEnforceMode
antrean ke IF_NOT_EXISTS untuk memastikan bahwa pemilihan rute tingkat antrean berlaku untuk tugas yang
pemilihannya tidak ada (misalnya, tugas BufferTask), tetapi mengikuti
pemilihan rute tugas yang memiliki spesifikasi pemilihan rutenya sendiri. Kemudian, gunakan
CreateTask. Metode ini memungkinkan Anda menentukan setiap pemilihan rute untuk tugas.
Pelajari cara membuat tugas dengan metode
CreateTask.
Antrean tanpa pemilihan rute tingkat antrean
Untuk antrean tanpa pemilihan rute tingkat antrean, buat tugas menggunakan metode CreateTask (atau konfigurasi pemilihan rute tingkat antrean). Metode CreateTask mencakup penentuan perutean untuk
tugas, yang diperlukan untuk antrean yang belum memiliki informasi
perutean di tingkat antrean. Pelajari cara membuat tugas dengan metode
CreateTask.
Memahami perutean tingkat antrean
Pemilihan rute tingkat antrean memungkinkan Anda menentukan informasi pemilihan rute default untuk tugas berdasarkan
antrean tempat tugas tersebut berada. Anda dapat menentukan apakah Anda ingin pemilihan rute default
hanya berlaku untuk tugas yang tidak memiliki informasi pemilihan rutenya sendiri, atau untuk semua
tugas.
Pilih pemilihan rute tingkat antrean jika Anda ingin:
Membuat tugas dengan permintaan HTTP standar (bukan dengan membuat objek
tugas)
Menggunakan antrean sebagai buffer di depan layanan target
Pendekatan per target
Pemilihan rute tingkat antrean berfungsi paling baik dengan arsitektur yang menggunakan satu antrean untuk setiap
layanan target. Dengan model ini, Anda dapat:
Mengelola tugas berdasarkan targetnya (misalnya, mengalihkan grup
tugas jika layanan target tidak aktif)
Mengirim semua tugas dalam antrean ke target yang sama
Membuat tugas tanpa membuat objek tugas secara eksplisit menggunakan
metode BufferTask
Mengaktifkan dan menonaktifkan perilaku penggantian tingkat antrean
Setelah mengonfigurasi pemilihan rute tingkat antrean, Anda dapat memutuskan kapan pemilihan rute tersebut diterapkan dengan menetapkan UriOverrideEnforceMode antrean.
IF_NOT_EXISTS: Pemilihan rute tingkat antrean hanya berlaku jika tugas tidak
berisi informasi pemilihan rutenya sendiri. Gunakan setelan ini jika Anda ingin antrean
dapat mengirim tugas ke target yang berbeda.
ALWAYS: Pemilihan rute tingkat antrean selalu berlaku, meskipun tugas berisi
informasi pemilihan rutenya sendiri. Setiap informasi pemilihan rute yang ditetapkan pada tingkat tugas akan diganti oleh pemilihan rute tingkat antrean. Gunakan setelan ini jika Anda ingin
mewajibkan tugas dalam antrean tertentu untuk menggunakan informasi pemilihan rute yang sama.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[],[],null,["# Choose your task creation method\n\nYou can create HTTP tasks using either the `BufferTask` method or the\n`CreateTask` method. This page provides guidance on how to choose between the\ntwo methods for creating HTTP tasks.\n\nThe `BufferTask` method lets you create a task by sending an HTTP request to the\nqueue. The `CreateTask` method lets you create a task by constructing the task\nobject. For most use cases, explicitly constructing the task object is\nunnecessary. The best method for creating tasks depends on whether your queue\nhas queue-level routing.\n\nBefore you begin\n----------------\n\nCheck the following list of use cases to see if any apply to you:\n\n- You want to create tasks using the RPC API or client libraries (rather than the REST API)\n- You want to create tasks as [App Engine task](/tasks/docs/dual-overview#appe) objects (rather than as the more typical task objects for generic HTTP targets which may or may not be App Engine targets)\n\nIf any of these apply to you, you don't need to choose your task creation method\nbecause only one method (the `CreateTask` method) is supported. Go directly to\nthe `CreateTask` documentation for your task type:\n\n- [HTTP tasks](/tasks/docs/creating-http-target-tasks#createtask_method)\n- [App Engine tasks](/tasks/docs/creating-appengine-tasks)\n\nCheck if your queue has queue-level routing\n-------------------------------------------\n\n1. Run the [gcloud tasks queues describe](/sdk/gcloud/reference/tasks/queues/describe)\n command in your terminal:\n\n ```bash\n gcloud tasks queues describe QUEUE_NAME \\\n --location=LOCATION\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e: the name of your queue.\n - \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e: the [region](/tasks/docs/locations) where your queue is located. If not specified, and if there is an associated app, the location of your project's [App Engine app](/appengine/docs/standard/managing-projects-apps-billing) is used.\n2. In the output, look for the field `httpTarget` and check if the\n `uriOverride` has been set.\n\n - If the output includes a line for `uriOverride` with a `host` specified,\n your queue has queue-level routing. To choose your task creation method, see\n the section [Queues with queue-level routing](#queue-level-routing).\n\n - If the output does not include a line for `uriOverride` or if the\n `uriOverride` description does not show a `host` specified, your queue does\n not have queue-level routing. To choose your task creation method, see the\n section [Queues without queue-level routing](#no-queue-level-routing) (or\n [configure queue-level routing](/tasks/docs/configuring-queues#configure_queue-level_routing_for_http_tasks)).\n\nQueues with queue-level routing\n-------------------------------\n\nFor queues with queue-level routing, the task creation method depends on the\ntask's routing requirements:\n\n- **Task has the same routing requirements as specified at the queue level:** Use `BufferTask`. Learn how to create tasks with the [`BufferTask`](/tasks/docs/creating-http-target-tasks#buffertask_method) method.\n- **Task has different routing requirements from those specified at the queue-level:** Set the queue's [UriOverrideEnforceMode](/tasks/docs/reference/rest/v2/projects.locations.queues#UriOverrideEnforceMode) to `IF_NOT_EXISTS` to ensure that the queue-level routing applies to tasks for which routing does not exist (for example, `BufferTask` tasks), but respects the routing of tasks that carry their own routing specifications. Then use `CreateTask`. This method lets you specify individual routing for the task. Learn how to create tasks with the [`CreateTask`](/tasks/docs/creating-http-target-tasks#createtask_method) method.\n\nQueues without queue-level routing\n----------------------------------\n\nFor queues without queue-level routing, create tasks by using the `CreateTask`\nmethod (or\n[configure](/tasks/docs/configuring-queues#configure_queue-level_routing_for_http_tasks)\nqueue-level routing). The `CreateTask` method includes specifying routing for\nthe task, which is required for queues that don't already have routing\ninformation at the queue-level. Learn how to create tasks with the\n[`CreateTask`](/tasks/docs/creating-http-target-tasks#createtask_method)\nmethod.\n\nUnderstand queue-level routing\n------------------------------\n\nQueue-level routing lets you specify default routing information for tasks based\non the queue they're in. You can specify whether you want the default routing to\napply only to tasks that don't have their own routing information, or to all\ntasks.\n\nChoose queue-level routing if you want to:\n\n- Create tasks with standard HTTP requests (rather than by constructing the task object)\n- Use a queue as a buffer in front of a target service\n\n| **Note:** In the case of App Engine targets, any routing done by Cloud Tasks is handled prior to any routing defined in an [App Engine dispatch file](/appengine/docs/flexible/how-requests-are-routed?tab=python#dispatch). This dispatch file lets you send incoming requests to a specific service based on the path or hostname in the request URL.\n\n### The per-target approach\n\nQueue-level routing works best with architectures that use one queue for each\ntarget service. This model lets you:\n\n- Manage tasks based on their target (for example, redirecting a group of tasks if the target service is down)\n- Send all tasks in a queue to the same target\n- Create tasks without explicitly constructing the task object by using the `BufferTask` method\n\n### Turning queue-level override behavior on and off\n\nOnce you configure queue-level routing, you can decide when it gets enforced by\nsetting the queue's\n[UriOverrideEnforceMode](/tasks/docs/reference/rest/v2/projects.locations.queues#UriOverrideEnforceMode).\n\n- `IF_NOT_EXISTS`: The queue-level routing applies only when a task doesn't contain its own routing information. Use this setting if you want the queue to be able to dispatch tasks to different targets.\n- `ALWAYS`: The queue-level routing always applies, even if a task contains its own routing information. Any routing information set at the task level is overridden by the queue-level routing. Use this setting if you want to require tasks in a given queue to use the same routing information."]]