Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tài liệu này cho biết cách tạo một lô gồm các tác vụ từ môi trường máy chủ bằng gRPC hoặc REST. Để biết thêm thông tin về cách tạo việc cần làm, hãy xem:
Các trường của việc cần làm để tạo việc cần làm theo lô
Khi tạo hàng loạt tác vụ, mỗi phần tử CreateTasksRequest trong requests phải tuân thủ các quy tắc xác thực giống như yêu cầu CreateTask đối với một tác vụ duy nhất, ngoại trừ việc các trường parent và header là không bắt buộc.
Nếu được đặt, các trường này phải giống với các trường tương ứng ở cấp cao nhất BatchCreateTasksRequest.
Để biết thêm thông tin, hãy xem tài liệu tham khảo API cho BatchCreateTasks đối với gRPC hoặc REST.
Các trường bắt buộc trong lô
Trường
Giá trị
yêu cầu
Array<CreateTasksRequest>
Các trường không bắt buộc của tác vụ hàng loạt
Trường
Giá trị
tiêu đề
DeliveryRequestHeader
Tạo một nhóm việc cần làm
Các ví dụ sau đây cho biết cách tạo cả nhiệm vụ lấy hàng và giao hàng bằng thư viện gRPC Java hoặc cách đưa ra yêu cầu REST HTTP đến BatchCreateTask. Hãy xem các phần tử JWT để biết cú pháp JWT chính xác.
gRPC
staticfinalStringPROJECT_ID="my-delivery-co-gcp-project";DeliveryServiceBlockingStubdeliveryService=DeliveryServiceGrpc.newBlockingStub(channel);// Delivery Task settingsTaskdeliveryTask=Task.newBuilder().setType(Task.Type.DELIVERY).setState(Task.State.OPEN).setTrackingId("delivery-tracking-id").setPlannedLocation(// Grand Indonesia East MallLocationInfo.newBuilder().setPoint(LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826))).setTaskDuration(Duration.newBuilder().setSeconds(2*60)).build();// Delivery Task requestCreateTaskRequestcreateDeliveryTaskRequest=CreateTaskRequest.newBuilder()// No need for the header or parent fields.setTaskId("task-8312508")// Task ID assigned by the Provider.setTask(deliveryTask)// Initial state.build();// Pickup Task settingsTaskpickupTask=Task.newBuilder().setType(Task.Type.PICKUP).setState(Task.State.OPEN).setTrackingId("pickup-tracking-id").setPlannedLocation(// Grand Indonesia East MallLocationInfo.newBuilder().setPoint(LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826))).setTaskDuration(Duration.newBuilder().setSeconds(2*60)).build();// Pickup Task requestCreateTaskRequestcreatePickupTaskRequest=CreateTaskRequest.newBuilder()// No need for the header or parent fields.setTaskId("task-8241890")// Task ID assigned by the Provider.setTask(pickupTask)// Initial state.build();// Batch Create Tasks settingsStringparent="providers/"+PROJECT_ID;// Batch Create Tasks requestBatchCreateTasksRequestbatchCreateTasksRequest=BatchCreateTasksRequest.newBuilder().setParent(parent).addRequests(createDeliveryTaskRequest).addRequests(createPickupTaskRequest).build();// Error handling// If Fleet Engine does not have any task(s) with these task ID(s) and the// credentials of the requestor pass, the service creates the task(s)// successfully.try{BatchCreateTasksResponsecreatedTasks=deliveryService.batchCreateTasks(batchCreateTasksRequest);}catch(StatusRuntimeExceptione){Statuss=e.getStatus();switch(s.getCode()){caseALREADY_EXISTS:break;casePERMISSION_DENIED:break;}return;}
REST
Để tạo một nhiệm vụ giao hàng và một nhiệm vụ đến lấy hàng trong môi trường máy chủ, hãy thực hiện lệnh gọi HTTP REST đến BatchCreateTasks:
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThis document explains how to create multiple tasks at once (batch creation) using the Fleet Engine Delivery API.\u003c/p\u003e\n"],["\u003cp\u003eYou can create tasks in batches by sending a \u003ccode\u003eBatchCreateTasksRequest\u003c/code\u003e containing multiple \u003ccode\u003eCreateTaskRequest\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eExamples are provided for creating tasks in batches using both gRPC and REST, including code snippets and a sample \u003ccode\u003ecurl\u003c/code\u003e command.\u003c/p\u003e\n"],["\u003cp\u003eWhen creating tasks in batches, certain fields are required (\u003ccode\u003erequests\u003c/code\u003e) while others are optional (\u003ccode\u003eheader\u003c/code\u003e, inheriting from top-level request if present).\u003c/p\u003e\n"],["\u003cp\u003eEach \u003ccode\u003eCreateTasksRequest\u003c/code\u003e element in the batch follows the same validation rules as individual task creation, with exceptions for optional \u003ccode\u003eparent\u003c/code\u003e and \u003ccode\u003eheader\u003c/code\u003e fields.\u003c/p\u003e\n"]]],["To create tasks in bulk from a server, use `BatchCreateTasks` via gRPC or REST. Each `CreateTasksRequest` within the `requests` array requires the same validation as a single `CreateTask` request. For example, create pickup and delivery tasks by defining task details like type, state, tracking ID, planned location, and duration. In gRPC, build `CreateTaskRequest` for each task, then a `BatchCreateTasksRequest` including an array of these requests. REST requires a POST request to `BatchCreateTasks` with a JSON payload of `BatchCreateTasksRequest`.\n"],null,["This document shows how to create a batch of tasks from a server environment\nusing gRPC or REST. For more details on creating tasks, see:\n\n- [Create shipment tasks](/maps/documentation/mobility/fleet-engine/journeys/tasks/create-shipment-tasks)\n- [Create other task types](/maps/documentation/mobility/fleet-engine/journeys/tasks/create-other-tasks)\n\nTask fields for creating tasks in batch\n\nWhen creating tasks in batch, each `CreateTasksRequest` element in `requests`\nmust pass the same validation rules as a `CreateTask` request for a single task,\nwith the exception that the `parent` and `header` fields are optional.\nIf set, they must be identical to their respective fields at the top level\n`BatchCreateTasksRequest`.\n\nFor more information, see the API Reference documentation for `BatchCreateTasks`\nfor [gRPC](/maps/documentation/mobility/fleet-engine/reference/tasks/rpc/maps.fleetengine.delivery.v1#maps.fleetengine.delivery.v1.DeliveryService) or [REST](/maps/documentation/mobility/fleet-engine/reference/tasks/rest/v1/providers.tasks/batchCreate).\n\nRequired batch fields\n\n| Field | Value |\n|----------|-----------------------------|\n| requests | `Array\u003cCreateTasksRequest\u003e` |\n\nOptional batch task fields\n\n| Field | Value |\n|--------|-------------------------|\n| header | `DeliveryRequestHeader` |\n\nCreate a batch of tasks\n\nThe following examples shows how to create both a pickup and a delivery task\nusing the [Java gRPC library](/maps/documentation/mobility/fleet-engine/essentials/client-libraries-tasks#java) or how to make an HTTP REST request to\n[`BatchCreateTask`](/maps/documentation/mobility/fleet-engine/reference/tasks/rest/v1/providers.tasks/batchCreate). See\n[JWT elements](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/jwt#jwt-elements)\nfor the correct JWT syntax. \n\ngRPC \n\n static final String PROJECT_ID = \"my-delivery-co-gcp-project\";\n\n DeliveryServiceBlockingStub deliveryService =\n DeliveryServiceGrpc.newBlockingStub(channel);\n\n // Delivery Task settings\n Task deliveryTask = Task.newBuilder()\n .setType(Task.Type.DELIVERY)\n .setState(Task.State.OPEN)\n .setTrackingId(\"delivery-tracking-id\")\n .setPlannedLocation( // Grand Indonesia East Mall\n LocationInfo.newBuilder().setPoint(\n LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826)))\n .setTaskDuration(\n Duration.newBuilder().setSeconds(2 * 60))\n .build();\n\n // Delivery Task request\n CreateTaskRequest createDeliveryTaskRequest =\n CreateTaskRequest.newBuilder() // No need for the header or parent fields\n .setTaskId(\"task-8312508\") // Task ID assigned by the Provider\n .setTask(deliveryTask) // Initial state\n .build();\n\n // Pickup Task settings\n Task pickupTask = Task.newBuilder()\n .setType(Task.Type.PICKUP)\n .setState(Task.State.OPEN)\n .setTrackingId(\"pickup-tracking-id\")\n .setPlannedLocation( // Grand Indonesia East Mall\n LocationInfo.newBuilder().setPoint(\n LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826)))\n .setTaskDuration(\n Duration.newBuilder().setSeconds(2 * 60))\n .build();\n\n // Pickup Task request\n CreateTaskRequest createPickupTaskRequest =\n CreateTaskRequest.newBuilder() // No need for the header or parent fields\n .setTaskId(\"task-8241890\") // Task ID assigned by the Provider\n .setTask(pickupTask) // Initial state\n .build();\n\n // Batch Create Tasks settings\n String parent = \"providers/\" + PROJECT_ID;\n\n // Batch Create Tasks request\n BatchCreateTasksRequest batchCreateTasksRequest =\n BatchCreateTasksRequest.newBuilder()\n .setParent(parent)\n .addRequests(createDeliveryTaskRequest)\n .addRequests(createPickupTaskRequest)\n .build();\n\n // Error handling\n // If Fleet Engine does not have any task(s) with these task ID(s) and the\n // credentials of the requestor pass, the service creates the task(s)\n // successfully.\n\n try {\n BatchCreateTasksResponse createdTasks = deliveryService.batchCreateTasks(\n batchCreateTasksRequest);\n } catch (StatusRuntimeException e) {\n Status s = e.getStatus();\n switch (s.getCode()) {\n case ALREADY_EXISTS:\n break;\n case PERMISSION_DENIED:\n break;\n }\n return;\n }\n\nREST\n\nTo create a delivery and a pickup task from a server environment, make an\nHTTP REST call to `BatchCreateTasks`: \n\n POST https://fleetengine.googleapis.com/v1/providers/\u003cproject_id\u003e/batchCreate\n\n*\\\u003cid\\\u003e* is a unique identifier for the task.\n\nThe request header must contain a field *Authorization* with the value\n*Bearer \\\u003ctoken\\\u003e* , where *\\\u003ctoken\\\u003e* is issued by your server\naccording to the guidelines described in [Service account roles](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts) and\n[JSON Web tokens](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/jwt).\n\nThe request body must contain a `BatchCreateTasksRequest` entity.\n\nExample `curl` command: \n\n # Set $JWT, $PROJECT_ID, $DELIVERY_TRACKING_ID, $DELIVERY_TASK_ID,\n # $PICKUP_TRACKING_ID, and $PICKUP_TASK_ID in the local environment\n curl -X POST \"https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/tasks:batchCreate\" \\\n -H \"Content-type: application/json\" \\\n -H \"Authorization: Bearer ${JWT}\" \\\n --data-binary @- \u003c\u003c EOM\n {\n \"requests\" : [\n {\n \"taskId\": \"${DELIVERY_TASK_ID}\",\n \"task\" : {\n \"type\": \"DELIVERY\",\n \"state\": \"OPEN\",\n \"trackingId\": \"${DELIVERY_TRACKING_ID}\",\n \"plannedLocation\": {\n \"point\": {\n \"latitude\": -6.195139,\n \"longitude\": 106.820826\n }\n },\n \"taskDuration\": \"90s\"\n }\n },\n {\n \"taskId\": \"${PICKUP_TASK_ID}\",\n \"task\" : {\n \"type\": \"PICKUP\",\n \"state\": \"OPEN\",\n \"trackingId\": \"${PICKUP_TRACKING_ID}\",\n \"plannedLocation\": {\n \"point\": {\n \"latitude\": -6.195139,\n \"longitude\": 106.820826\n }\n },\n \"taskDuration\": \"90s\"\n }\n }\n ]\n }\n EOM\n\n| **Experimental:** As an experimental feature, the `LocationInfo` ([gRPC](/maps/documentation/mobility/fleet-engine/reference/tasks/rpc/maps.fleetengine.delivery.v1#locationinfo) or [REST](/maps/documentation/mobility/fleet-engine/reference/tasks/rest/v1/LocationInfo)) field now supports using `place` ([gRPC](/maps/documentation/mobility/fleet-engine/reference/tasks/rpc/maps.fleetengine.delivery.v1#maps.fleetengine.delivery.v1.LocationInfo.FIELDS.string.maps.fleetengine.delivery.v1.LocationInfo.place) or [REST](/maps/documentation/mobility/fleet-engine/reference/tasks/rest/v1/LocationInfo#FIELDS.place)), either alongside or instead of `LatLng`.\n\nWhat's next\n\n- [Configure tasks](/maps/documentation/mobility/fleet-engine/journeys/tasks/configure-tasks)\n- [Update tasks](/maps/documentation/mobility/fleet-engine/journeys/tasks/update-tasks)"]]