Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Puoi creare attività HTTP utilizzando il metodo BufferTask o il metodo
CreateTask. Questa pagina fornisce indicazioni su come scegliere tra i due metodi per la creazione di attività HTTP.
Il metodo BufferTask consente di creare un'attività inviando una richiesta HTTP alla fila. Il metodo CreateTask consente di creare un'attività costruendo l'oggetto
attività. Per la maggior parte dei casi d'uso, la creazione esplicita dell'oggetto attività non è necessaria. Il metodo migliore per creare attività dipende dal fatto che la coda abbia o meno il routing a livello di coda.
Prima di iniziare
Controlla il seguente elenco di casi d'uso per verificare se uno di questi si applica al tuo caso:
Vuoi creare attività utilizzando l'API RPC o le librerie client (anziché
l'API REST)
Vuoi creare attività come oggetti
Attività App Engine (anziché come
gli oggetti attività più tipici per i target HTTP generici che possono o meno essere
target App Engine)
Se uno di questi casi si applica al tuo caso, non devi scegliere il metodo di creazione delle attività perché è supportato un solo metodo (il metodo CreateTask). Vai direttamente alla documentazione CreateTask per il tuo tipo di attività:
LOCATION: la regione
in cui si trova la coda. Se non è specificato e se è presente un'app associata, viene utilizzata la posizione dell'app App Engine del progetto.
Nell'output, cerca il campo httpTarget e controlla se è stato impostato uriOverride.
Se l'output include una riga per uriOverride con un host specificato,
la coda ha il routing a livello di coda. Per scegliere il metodo di creazione delle attività, consulta la sezione Code di coda con routing a livello di coda.
Se l'output non include una riga per uriOverride o se la descrizione di uriOverride non mostra un host specificato, la coda non ha il routing a livello di coda. Per scegliere il metodo di creazione delle attività, consulta la sezione Code senza routing a livello di coda (o configura il routing a livello di coda).
Code con routing a livello di coda
Per le code con routing a livello di coda, il metodo di creazione delle attività dipende dai requisiti di routing dell'attività:
La task ha gli stessi requisiti di instradamento specificati a livello di coda:
Utilizza BufferTask. Scopri come creare attività con il metodo
BufferTask.
L'attività ha requisiti di instradamento diversi da quelli specificati a livello di coda: imposta UriOverrideEnforceMode della coda su IF_NOT_EXISTS per assicurarti che l'instradamento a livello di coda venga applicato alle attività per le quali non esiste un instradamento (ad esempio le attività BufferTask), ma rispetta l'instradamento delle attività che hanno le proprie specifiche di instradamento. Poi usa
CreateTask. Questo metodo ti consente di specificare il routing individuale per l'attività.
Scopri come creare attività con il metodo
CreateTask.
Code senza routing a livello di coda
Per le code senza routing a livello di coda, crea le attività utilizzando il metodo CreateTask (o
configura
il routing a livello di coda). Il metodo CreateTask include la specifica del routing per
la task, che è obbligatoria per le code che non dispongono già di informazioni sul routing
a livello di coda. Scopri come creare attività con il metodo
CreateTask.
Informazioni sul routing a livello di coda
Il routing a livello di coda consente di specificare le informazioni di routing predefinite per le attività in base alla coda in cui si trovano. Puoi specificare se vuoi che il routing predefinito sia applicato solo alle attività che non dispongono di informazioni di routing proprie o a tutte le attività.
Scegli il routing a livello di coda se vuoi:
Creare attività con richieste HTTP standard (anziché con la costruzione dell'oggetto attività)
Utilizzare una coda come buffer davanti a un servizio di destinazione
L'approccio per target
Il routing a livello di coda funziona meglio con le architetture che utilizzano una coda per ogni servizio di destinazione. Questo modello ti consente di:
Gestire le attività in base al target (ad esempio, reindirizzare un gruppo di attività se il servizio di destinazione non è attivo)
Invia tutte le attività in una coda allo stesso target
Creare attività senza costruire esplicitamente l'oggetto attività utilizzando il metodo
BufferTask
Attivare e disattivare il comportamento di override a livello di coda
Dopo aver configurato il routing a livello di coda, puoi decidere quando viene applicato impostando UriOverrideEnforceMode della coda.
IF_NOT_EXISTS: il routing a livello di coda si applica solo quando un'attività non contiene le proprie informazioni di routing. Utilizza questa impostazione se vuoi che la coda possa inviare attività a target diversi.
ALWAYS: il routing a livello di coda viene sempre applicato, anche se un'attività contiene le proprie informazioni di routing. Eventuali informazioni di instradamento impostate a livello di attività vengono sostituite dall'instradamento a livello di coda. Utilizza questa impostazione se vuoi richiedere alle attività di una determinata coda di utilizzare le stesse informazioni di instradamento.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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."]]