[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eThe App Engine Cron Service allows the configuration of regularly scheduled tasks, known as cron jobs, which are triggered automatically to perform actions like sending email reports or updating cached data.\u003c/p\u003e\n"],["\u003cp\u003eCron jobs are configured in a \u003ccode\u003ecron.yaml\u003c/code\u003e file within the application's root directory, and they operate by making scheduled HTTP \u003ccode\u003eGET\u003c/code\u003e requests to specified endpoints within the same app.\u003c/p\u003e\n"],["\u003cp\u003eTo create a cron job, you define the job in \u003ccode\u003ecron.yaml\u003c/code\u003e, specifying elements like the URL to be called and the schedule, and then you must create a handler in the app for the cron job URL to execute the desired tasks.\u003c/p\u003e\n"],["\u003cp\u003eCron job requests that fail (returning a status code outside 200-299) can be configured for retries by adding a \u003ccode\u003eretry_parameters\u003c/code\u003e block to the \u003ccode\u003ecron.yaml\u003c/code\u003e file, allowing control over the number of retries and backoff times.\u003c/p\u003e\n"],["\u003cp\u003eSecurity for cron job URLs can be managed by restricting access to administrator accounts via the \u003ccode\u003elogin: admin\u003c/code\u003e setting in \u003ccode\u003eapp.yaml\u003c/code\u003e, and the presence of the \u003ccode\u003eX-Appengine-Cron: true\u003c/code\u003e header indicates a request is from the Cron Service.\u003c/p\u003e\n"]]],[],null,["# Scheduling Tasks With Cron for Java 8 (gcloud CLI-based)\n\n| **Note:** This page uses Cron with the YAML cron configuration file, which you use if you use tooling based on the gcloud CLI, such as the gcloud CLI, or gcloud CLI-based [Maven](/appengine/docs/legacy/standard/java/using-maven), [Gradle](/appengine/docs/legacy/standard/java/using-gradle), [Eclipse](/eclipse/docs), or [IntelliJ](/tools/intellij/docs) plugins. The XML cron configuration file using the AppCfg toolset delivered with the standalone App Engine SDK has been [shut down](/appengine/docs/legacy/standard/java/sdk-gcloud-migration).\n\nThe App Engine Cron Service allows you to configure regularly scheduled tasks\nthat operate at defined times or regular intervals. These tasks are commonly\nknown as *cron jobs*. These cron jobs are automatically triggered by the\nApp Engine Cron Service. For instance, you might use a cron job to send\nout an email report on a daily basis, or to update some cached data every 10\nminutes, or refresh summary information once an hour.\n\nA cron job makes a scheduled HTTP `GET` request to the specified endpoint in the\nsame app where the cron job is configured. The handler for that endpoint executes\nthe logic when it is called.\n\nThe App Engine Cron Service cannot be used to call web endpoints outside the\nApp Engine host app. It cannot be used to call App Engine\nendpoints from other apps besides the host app.\n\nA cron job request is subject to the same limits as those for\n[push task queues](/appengine/docs/quotas#Task_Queue).\n\nBefore you begin\n----------------\n\nTo deploy or update schedules, your account requires one of the following\nIAM roles:\n\n- Owner\n- Editor\n\nYou can set the permission on the\n[IAM page in the Google Cloud console](https://console.cloud.google.com/iam-admin/iam).\n\nCreating a cron job\n-------------------\n\n1. Create the `cron.yaml` file in the root directory of your application (alongside `app.yaml`).\n2. Add one or more `\u003ccron\u003e` entries to your file and define the\n necessary elements for your job, including the required `\u003curl\u003e` and\n `\u003cschedule\u003e` elements. [Review the cron.yaml syntax and\n options](/appengine/docs/legacy/standard/java/config/cronref-yaml)\n for more details about the elements of the `cron.yaml` file.\n\n The following example creates a basic cron job that runs daily: \n\n cron:\n - description: \"daily summary job\"\n url: /tasks/summary\n target: beta\n schedule: every 24 hours\n\n The target specification is optional and is the name of a service/version.\n If present, the target is prepended to your app's hostname, causing the job\n to be routed to that service/version.\n If no target is specified, the job will run in the versions of the `default`\n service that are configured for traffic.\n3. Create a handler for the cron job URL. The handler should execute any tasks\n that you want scheduled. The handler should respond with an HTTP status code\n between 200 and 299 (inclusive) to indicate success. Other status codes can\n be returned and can be used to\n [retry the cron job](/appengine/docs/legacy/standard/java/config/cronref#retry).\n\nThe handler can be as simple as a Servlet in the app. The Servlet URL mapping in `web.xml` should be the same as the cron job URL.\n\nTesting cron jobs in the development server\n-------------------------------------------\n\nThe local development server doesn't automatically run your cron jobs. You can\nmake requests directly to your cron job's URL to test your functionality. You\ncan use your local cron or scheduled tasks interface to trigger the URLs of your\njobs with [curl](http://curl.haxx.se/) or a similar tool.\n\nRetrying cron jobs that fail\n----------------------------\n\nIf a cron job's request handler returns a status code that is not in the range\n200--299 (inclusive) App Engine considers the job to have failed. By default,\nfailed jobs are not retried unless a 503 status code is returned, in which case\nit is retried every minute until it succeeds or returns a 200-299 status code.\n\nTo set failed jobs to be retried:\n\n1. Include a `retry_parameters` block in your `cron.yaml` file.\n2. Choose and set the\n [retry parameters](/appengine/docs/legacy/standard/java/config/cronref-yaml#retry) in the\n `retry_parameters` block.\n\n For example, this sample `cron.yaml` file contains a single cron job that is\n configured to retry up to five times (the default) with a starting backoff\n of 2.5 seconds that doubles each time. \n\n cron:\n - description: \"retry demo\"\n url: /retry\n schedule: every 10 mins\n retry_parameters:\n min_backoff_seconds: 2.5\n max_doublings: 5\n\n[Learn more about the cron retry\noptions](/appengine/docs/legacy/standard/java/config/cronref-yaml#retry).\n\nDeploying cron jobs\n-------------------\n\nTo deploy the cron jobs specified in your `cron.yaml` configuration file,\nrun the following command: \n\n### gcloud\n\n gcloud app deploy cron.yaml\n\n### Maven\n\n mvn appengine:deployCron cron.yaml\n\n### Gradle\n\n gradle appengineDeployCron cron.yaml\n\n### IDE\n\nIf you use [IntelliJ](/tools/intellij/docs/deploy-std) or\n[Eclipse](/eclipse/docs/deploying),\nselect the individual configuration files to be deployed using the\ndeployment form.\n\nDeleting all cron jobs\n----------------------\n\nTo delete all cron jobs:\n\n1. Edit the contents of the\n `cron.yaml` file to:\n\n cron:\n\n2. Deploy the `cron.yaml` file to App Engine.\n\nSecuring URLs for cron\n----------------------\n\nA cron handler is just a normal handler defined in `app.yaml`. You can prevent\nusers from accessing URLs used by scheduled tasks by restricting access to\nadministrator accounts. Scheduled tasks can access admin-only URLs. You can\nrestrict a URL by adding `login: admin` to the handler configuration in\n`app.yaml`.\n\nAn example might look like this in `app.yaml`: \n\n application: hello-cron\n version: 1\n runtime: java\n api_version: 1\n\n handlers:\n - url: /report/weekly\n servlet: mysite.server.CronServlet\n login: admin\n\n| **Note:** While cron jobs can use URL paths restricted with `login: admin`, they *cannot* use URL paths restricted with `login: required` because cron scheduled tasks are not run as any user. The `admin` restriction is satisfied by the inclusion of the `X-Appengine-Cron` header described below.\n\nTo test a cron job, sign in as an administrator and visit the URL of the handler\nin your browser.\n\nRequests from the Cron Service will also contain a HTTP header: \n\n X-Appengine-Cron: true\n\nThe `X-Appengine-Cron` header is set internally by App Engine. If your\nrequest handler finds this header it can trust that the request is a cron\nrequest. If the header is present in an external user request to your app, it\nis stripped, except for requests from logged in administrators of the\napplication, who are allowed to set the header for testing purposes.\n\nApp Engine issues Cron requests from the IP address\n`0.1.0.2`. For Cron jobs created with older gcloud versions (earlier than\n326.0.0), Cron requests will come from `0.1.0.1`.\n\nCalling Google Cloud Endpoints\n------------------------------\n\nYou cannot specify a [Google Cloud Endpoint](/appengine/features#Endpoints) in\nthe `url` field of a cron job.\nIf you want your cron job to call a Google Cloud Endpoint,\nissue a request to a target that is served by a handler in\nyour app, and call the endpoint class and method from the handler code.\n\nViewing cron jobs in the Google Cloud console\n---------------------------------------------\n\nYou can view scheduled cron jobs in\n[Cloud Scheduler](https://console.cloud.google.com/cloudscheduler)'s **App Engine Cron Jobs**\ntab.\n\nYou can also [view logs](https://console.cloud.google.com/logs) to see when cron jobs were added or removed.\n\nLearn more\n----------\n\nSee detailed information about defining cron jobs in the [cron.yaml\nReference](/appengine/docs/legacy/standard/java/config/cronref-yaml)."]]