[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eThis document details the process of creating a task handler, which is essentially a request handler designed to process tasks sent by the Task Queue service.\u003c/p\u003e\n"],["\u003cp\u003eTask handlers must return an HTTP status code between 200-299 to signal success, and any other code will indicate failure and trigger a retry, with the Task Queue service sending requests from the IP address \u003ccode\u003e0.1.0.2\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTask handlers must be idempotent due to the Task Queue API's "at least once" delivery design, meaning they must be able to be run multiple times without harmful side effects.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine adds specific HTTP headers to task requests, such as \u003ccode\u003eX-Appengine-QueueName\u003c/code\u003e, \u003ccode\u003eX-Appengine-TaskName\u003c/code\u003e, and \u003ccode\u003eX-Appengine-TaskRetryCount\u003c/code\u003e, to provide the handler with task-specific information, that can be used to determine if the request is a task queue request.\u003c/p\u003e\n"],["\u003cp\u003eFor security, task handler URLs that perform sensitive operations can be restricted to App Engine administrators by configuring the \u003ccode\u003eweb.xml\u003c/code\u003e file, which would prevent direct access by external users.\u003c/p\u003e\n"]]],[],null,["# Creating Task Handlers\n\nThis page describes how to create a *task handler* , the code that handles a push\ntask. You must provide a request handler to process the task. The mapping from\nthe request URL to the appropriate handler is declared in your service's `web.xml`, just\nlike any other request handler. Because you control how to map task requests to\na handler, you're free to organize your task handlers. If your application\nprocesses many different kinds of tasks, you can add all the handlers to a\nsingle service, or you can distribute them among multiple services.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| java-gen2\n|\n| /services/access). If you are updating to the App Engine Java 11/17 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/java-differences) to learn about your migration options for legacy bundled services.\n\nWriting a push task request handler\n-----------------------------------\n\nIn the queue, the Task Queue service creates an HTTP header and sends it to an\ninstance of the worker service specified by the task's target. Task Queue requests are\nsent from the IP address `0.1.0.2`.\n\nYour handler does not need to be written in the same language that created and\nenqueued the task if the handler is in a\n[separate service](/appengine/docs/legacy/standard/java/an-overview-of-app-engine).\n\nWhen you write your handler, follow these guidelines:\n\n- The code must return an HTTP status code within the range 200--299 to\n indicate success. Any other code indicates that the task failed.\n\n | **Note:** App Engine itself returns a `503` status code when instances are overloaded or otherwise unavailable. The Task Queue service responds to this by slowing delivery from queues to handlers, to avoid making the problem worse. If you wish to trigger a retry intentionally, use any status code *other* than `2xx` or `503`.\n- Push tasks have a fixed [completion\n deadline](/appengine/docs/legacy/standard/java/taskqueue/push#the_task_deadline)\n that depends on the scaling type of the service that's running them.\n Automatic scaling services must finish before 10 minutes have elapsed.\n Manual and basic scaling services can run up to 24 hours. If your handler\n misses the deadline, the Task Queue service assumes the task failed and\n will retry it.\n\n\n When a task's execution time nears the deadline, App Engine raises a\n [`DeadlineExceededException`](/appengine/docs/legacy/standard/java/javadoc/com/google/apphosting/api/DeadlineExceededException)\n before the deadline is reached, so you can save your work or log whatever\n progress was made.\n- The handler must be [idempotent](https://wikipedia.org/wiki/idempotent).\n App Engine's Task Queue API is designed to provide \"at least once\"\n delivery; that is, if a task is successfully added, App Engine will deliver\n it to a handler at least once. Note that in some rare circumstances, multiple task\n execution is possible, so your code must ensure that there are no harmful\n side-effects of repeated execution.\n\nTask Queue uses the HTTP code in the handler's response to determine if the\ntask succeeded. The response from the handler is seen only by the Task Queue service\nand only to determine if the task succeeded. The queue ignores all other fields\nin the response. Then the service discards the response. The originating app **never\nreceives** any of the data. If a task fails, the Task Queue service retries the\ntask by sending another request.\n\nUser-supplied data can be delivered to the handler in the request as a query string or as a\npayload in the request body. Inserting user data is described in\n[Creating Tasks](/appengine/docs/legacy/standard/java/taskqueue/push/creating-tasks#adding_user_data). If the request includes data, the handler must know how\nit was inserted into the request. The exact code you use to fetch the data from\nthe request depends on the particular web framework you're using.\n\nTo test a task handler, sign in as an administrator and visit the handler's URL in your browser.\n\nReading request headers\n-----------------------\n\nA push task HTTP request has special headers set by App Engine, which contain\ntask-specific information your handler can use.\n\nIf these headers are present in an external user request to your app, they are stripped\nand replaced. The sole exception is for requests from logged-in administrators\nof the application, who are allowed to set headers for testing purposes. On the other hand, headers\nare not removed when your app is running in the development server.\n\nRequests from Task Queue will always contain the following headers:\n\nIf your request handler finds any of the headers listed above, it can trust that\nthe request is a Task Queue request.\n\nIn addition, requests from Task Queue can contain the following headers:\n\nSecuring task handler URLs\n--------------------------\n\nIf a task performs sensitive operations (such as modifying data), you might want to secure the handler URL to prevent a malicious external user from calling it directly. You can prevent users from accessing task URLs by restricting access to\n[App Engine administrators](/appengine/docs/legacy/standard/java/access-control). Task requests themselves are\nissued by App Engine and can always target a restricted URL.\n\nYou can read about restricting URLs at [Security and\nAuthentication](/appengine/docs/legacy/standard/java/config/webxml#Security_and_Authentication).\nAn example you would use in `web.xml` to restrict everything starting with\n`/tasks/` to admin-only is: \n\n \u003csecurity-constraint\u003e\n \u003cweb-resource-collection\u003e\n \u003cweb-resource-name\u003etasks\u003c/web-resource-name\u003e\n \u003curl-pattern\u003e/tasks/*\u003c/url-pattern\u003e\n \u003c/web-resource-collection\u003e\n \u003cauth-constraint\u003e\n \u003crole-name\u003eadmin\u003c/role-name\u003e\n \u003c/auth-constraint\u003e\n \u003c/security-constraint\u003e\n\nFor more on the format of `web.xml`, see the documentation for the\n[the deployment descriptor](/appengine/docs/legacy/standard/java/config/webxml).\n| **Note:** While tasks can push to URL paths restricted with `\u003crole-name\u003eadmin\u003c/role-name\u003e`, they *cannot* use URL paths restricted with `\u003crole-name\u003e*\u003c/role-name\u003e` because tasks are not run as any user. The `admin` restriction is satisfied by the inclusion of the `X-Appengine-QueueName` header described in [Reading request headers](#reading_request_headers).\n\nWhat's next\n-----------\n\n- Learn how to [delete tasks](/appengine/docs/legacy/standard/java/taskqueue/push/deleting-tasks-and-queues)"]]