Anda di halaman 1dari 3

1/10/14

Google App Engine Google Developers

Scheduled Tasks With Cron for Java


The App Engine Cron Service allows you to configure regularly scheduled tasks that operate at defined times or regular intervals. These tasks are commonly known as cron jobs . These cron jobs are automatically triggered by the App Engine Cron Service. For instance, you might use this to send out a report email on a daily basis, to update some cached data every 10 minutes, or to update some summary information once an hour. A cron job will invoke a URL, using an HTTP GET request, at a given time of day. An HTTP request invoked by cron can run for up to 10 minutes, but is subject to the same limits as other HTTP requests. Free applications can have up to 20 scheduled tasks. Paid applications can have up to 100 scheduled tasks. About cron.xml The schedule format Securing URLs for cron Cron and app versions Uploading cron jobs Cron support in the Admin Console Cron support in the development server

About cron.xml
Ac r o n . x m lfile in the W E B I N Fdirectory of your application (alongside a p p e n g i n e w e b . x m l ) controls cron for your Java application. The following is an example c r o n . x m lfile: < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " U T F 8 " ? > < c r o n e n t r i e s > < c r o n > < u r l > / r e c a c h e < / u r l > < d e s c r i p t i o n > R e p o p u l a t et h ec a c h ee v e r y2m i n u t e s < / d e s c r i p t i o n > < s c h e d u l e > e v e r y2m i n u t e s < / s c h e d u l e > < / c r o n > < c r o n > < u r l > / w e e k l y r e p o r t < / u r l > < d e s c r i p t i o n > M a i lo u taw e e k l yr e p o r t < / d e s c r i p t i o n > < s c h e d u l e > e v e r ym o n d a y0 8 : 3 0 < / s c h e d u l e > < t i m e z o n e > A m e r i c a / N e w _ Y o r k < / t i m e z o n e > < / c r o n > < c r o n > < u r l > / w e e k l y r e p o r t < / u r l > < d e s c r i p t i o n > M a i lo u taw e e k l yr e p o r t < / d e s c r i p t i o n > < s c h e d u l e > e v e r ym o n d a y0 8 : 3 0 < / s c h e d u l e > < t i m e z o n e > A m e r i c a / N e w _ Y o r k < / t i m e z o n e > < t a r g e t > v e r s i o n 2 < / t a r g e t > < / c r o n > < / c r o n e n t r i e s > For an XSD describing the format, check the file d o c s / c r o n . x s din the SDK. Ac r o n . x m lfile consists of a number of job definitions. A job definition must have a < u r l >and a < s c h e d u l e > . You can also optionally specify a < d e s c r i p t i o n > , < t i m e z o n e > , and a < t a r g e t > . The description is visible in the Admin Console. The u r lurl field is just a URL in your application. If the u r lelement contains the special XML characters & ,< ,> ,' , or " , you should escape them. The format of the s c h e d u l efield is covered more in The Schedule Format. The t i m e z o n eshould be the name of a standard zoneinfo time zone name. If you don't specify a timezone, jobs run in UTC (also known as GMT). The t a r g e tspecifies the version of your application on which to invoke the URL. See About appengine-web.xml for more information about application versions.

The schedule format


Cron schedules are specified using a simple English-like format. The following are examples of schedules:

https://developers.google.com/appengine/docs/java/config/cron

1/3

1/10/14
e v e r y1 2h o u r s e v e r y5m i n u t e sf r o m1 0 : 0 0t o1 4 : 0 0 2 n d , t h i r dm o n , w e d , t h uo fm a r c h1 7 : 0 0 e v e r ym o n d a y0 9 : 0 0 1 s tm o n d a yo fs e p , o c t , n o v1 7 : 0 0 e v e r yd a y0 0 : 0 0

Google App Engine Google Developers

If you don't need to run a recurring job at a specific time, but instead only need to run it at regular intervals, use the form: e v e r yN( h o u r s | m i n s | m i n u t e s )[ " f r o m "( t i m e )" t o "( t i m e ) ] The brackets are for illustration only, and quotes indicate a literal. N specifies a number. hours or minutes (you can also use mins ) specifies the unit of time. time specifies a time of day, as HH:MM in 24 hour time. By default, an interval schedule starts the next interval after the last job has completed. If a from...to clause is specified, however, the jobs are scheduled at regular intervals independent of when the last job completed. For example: e v e r y2h o u r sf r o m1 0 : 0 0t o1 4 : 0 0 This schedule runs the job three times per day at 10:00, 12:00, and 14:00, regardless of how long it takes to complete. You can use the literal "synchronized" as a synonym for from 00:00 to 23:59: e v e r y2h o u r ss y n c h r o n i z e d If you want more specific timing, you can specify the schedule as: ( " e v e r y " | o r d i n a l )( d a y s )[ " o f "( m o n t h s p e c ) ]( t i m e ) Where: ordinal specifies a comma separated list of "1st", "first" and so forth (both forms are ok) days specifies a comma separated list of days of the week (for example, "mon", "tuesday", with both short and long forms being accepted); "every day" is equivalent to "every mon,tue,wed,thu,fri,sat,sun" monthspec specifies a comma separated list of month names (for example, "jan", "march", "sep"). If omitted, implies every month. You can also say "month" to mean every month, as in "1,8,15,22 of month 09:00". time specifies the time of day, as HH:MM in 24 hour time.

Securing URLs for cron


You can prevent users from accessing URLs used by scheduled tasks by restricting access to administrator accounts. Scheduled tasks can access admin-only URLs. You can read about restricting URLs at Security and Authentication. An example you would use in w e b . x m lto restrict everything starting with / c r o n /to admin-only is: < s e c u r i t y c o n s t r a i n t > < w e b r e s o u r c e c o l l e c t i o n > < w e b r e s o u r c e n a m e > c r o n < / w e b r e s o u r c e n a m e > < u r l p a t t e r n > / c r o n / * < / u r l p a t t e r n > < / w e b r e s o u r c e c o l l e c t i o n > < a u t h c o n s t r a i n t > < r o l e n a m e > a d m i n < / r o l e n a m e > < / a u t h c o n s t r a i n t > < / s e c u r i t y c o n s t r a i n t >

Note: While cron jobs can use URL paths restricted with < r o l e n a m e > a d m i n < / r o l e n a m e > , they cannot use URL paths restricted with < r o l e n a m e > * < / r o l e n a m e > . For more on the format of w e b . x m l , see the documentation on the deployment descriptor. To test a cron job, sign in as an administrator and visit the URL of the handler in your browser. Requests from the Cron Service will also contain a HTTP header:

https://developers.google.com/appengine/docs/java/config/cron

2/3

1/10/14
X A p p E n g i n e C r o n :t r u e

Google App Engine Google Developers

The X A p p E n g i n e C r o nheader is set internally by Google App Engine. If your request handler finds this header it can trust that the request is a cron request. If the header is present in an external user request to your app, it is stripped. The exception being requests from logged in administrators of the application, who are allowed to set the header for testing purposes. Google App Engine issues Cron requests from the IP address 0.1.0.1.

Cron and app versions


If the t a r g e tparameter has been set for a job, the request is sent to the specified version. Otherwise Cron requests are sent to the default version of the application.

Uploading cron jobs


You can use A p p C f gto upload cron jobs. When you upload your application to App Engine using A p p C f gu p d a t e , the Cron Service is updated with the contents of c r o n . x m l . You can update just the cron configuration without uploading the rest of the application using A p p C f gu p d a t e _ c r o n . To delete all cron jobs, change the cron.xml file to just contain: < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " U T F 8 " ? > < c r o n e n t r i e s / >

Cron support in the Admin Console


The Admin Console allows you to view the state of your cron jobs. Select the "Cron Jobs" link from the side menu to view the state of the jobs, including the last time the job was run and the result of the job. You can also see when cron jobs are added or removed by selecting the "Admin Logs" page from the Admin Console menu.

Cron support in the development server


The development server doesn't automatically run your cron jobs. You can use your local desktop's cron or scheduled tasks interface to trigger the URLs of your jobs with curl or a similar tool.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Last updated November 19, 2013.

https://developers.google.com/appengine/docs/java/config/cron

3/3

Anda mungkin juga menyukai