Reproduced Quartz Job scheduling Framework

Source: Internet
Author: User

Reprinted from http://yangpanwww.iteye.com/blog/797563

Quartz is an open-source job scheduling framework that is written entirely in Java and designed for use in J2SE and Java EE applications. It provides a great deal of flexibility without sacrificing simplicity. You can use it to create simple or complex schedules for executing a job. The system integrates Quartz with Spring.

Quartz:

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.quartz-scheduler/quartz/1.7.3

First download the package: Quartz-1.7.3.jar (I uploaded the attachment, hehe)

Put the bag in Lib.

Applicationcontext.xml:

XML code
  1. <!--Timer Schedule--
  2. <!--objects to dispatch--
  3. <Bean id= "jobbean" class="net.xsbiz.common.MakeHtml" />
  4. <Bean id= "jobdetail" class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
  5. <property name= "targetObject" ref="Jobbean" />
  6. <property name="Targetmethod" value="execute" />
  7. <!--set concurrency to false-->
  8. <property name="concurrent" value= "false" />
  9. </Bean>
  10. <Bean id= "trigger" class="Org.springframework.scheduling.quartz.CronTriggerBean" >
  11. <property name= "jobdetail" ref="Jobdetail" />
  12. <!--expression, I do it every 30 --
  13. <property name="cronexpression" value= "0/30 * * * *?" />
  14. </Bean>
  15. <!--general Management class if you lazy-init= ' false ' then the container starts executing the scheduler--
  16. <Bean id= "startquertz" class="Org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" >
  17. <property name="triggers">
  18. <list>
  19. <!--Job Scheduler, List can join other schedulers--
  20. <ref bean="trigger" />
  21. </list>
  22. </Property>
  23. </Bean>

Xml:

XML code
  1. <!--set up spring monitoring, project start-up time is initialized --
  2. <listener>
  3. <listener-class>org.springframework.web.context.contextloaderlistener</listener-class >
  4. </Listener>
  5. <!--Specify the path to the spring configuration file--
  6. <context-param>
  7. <param-name>contextconfiglocation</param-name>
  8. <param-value>/web-inf/classes/applicationcontext.xml</param-value>
  9. </context-param>

Makehtml.java:

Java code
  1. The class that is called
  2. Public class Makehtml {
  3. //method to invoke
  4. public Void execute () {
  5. //What needs to be done
  6. }
  7. public static void Main (string[] args) {
  8. System.out.println ("----BEGIN---");
  9. ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
  10. //If the Startquertz Bean's lazy-init is set to False in the configuration file, it will not be instantiated
  11. Context.getbean ("Startquertz");
  12. System.out.print ("----End---");
  13. }
  14. }

1, Jobdetail:jobdetail is a specific class.

2. Trigger: Trigger, which is used to define when the Job executes. The most commonly used are Simpletrigger and Crontrigger. In general, if you need a fixed time and number of repetitions or a fixed interval, then Simpletrigger is more appropriate, if you have a lot of complex job scheduling, then crontrigger more appropriate. The cron mechanism of Crontrigger and Unix is basically the same, all we need is a cron expression. Like "0 0 12 * *?" "Will trigger execution at 12 o'clock noon every day;" 0 15 10? * 6L triggers Job execution at the last Friday 10:15 of each month.

3, Scheduler and Schedulerfactory:scheduler responsible for the management of Trigger, dispatch Job, Schedulerfactory is Scheduler factory, responsible for generating Scheduler.

Basically it's easy to implement, just an expression. A little. I went to the internet to search the next. Organize and put it to the following:

Allowed special characters for field name allowed values

0-59 , - * /
0-59 , - * /
小时 0-23 , - * /
1-31 , - * ? / L W C
1-12 or JAN-DEC , - * /
周几 1-7 or SUN-SAT , - * ? / L C #
年 (可选字段) empty, 1970-2099 , - * /

The ' * ' character can be used for all fields and set to "*" in the "sub" field to mean "every minute".

The '? ' character can be used in the "Day" and "Week" fields. It is used to specify ' ambiguous values '. This is used when you need to specify one of these two fields instead of the other. In the following example, you can see its meaning.

The '-' character is used to specify a range of values, such as "10-12" in the "Hour" field for "10 point to 12 points".

The ', ' character specifies a number of values. For example, in the "Weeks" field is set to "Mon,wed,fri" means "The days Monday, Wednesday, and Friday".

The '/' character is used to specify the increment of a value. For example, the "Seconds" field is set to "0/15" for "No. 0, 15, 30, and 45 seconds". "5/15" means "5th, 20, 35, and 50". The "*" character in front of '/' is equivalent to the specified starting at 0 seconds. Each field has a series of values that can start or end. For the seconds and minutes fields, the values range from 0 to 59, which is 0 to 23 for the hour field, 0 to 31 for the day field, and 1 to 12 for the month field. The "/" field is simply a value that helps you to start "nth" within the allowable numeric range. So for the "month" field, "7/6" is only meant to be opened in July instead of "every six months", please note the subtle differences.

The ' L ' character can be used in both the "Day" and "Week" fields. It is the abbreviation for "last", but it has different meanings in both fields. For example, "L" in the day field means "the last day of one months"-for January is number 31st for February is 28th (not a leap year). In the "Weeks" field, it simply represents "7" or "SAT", but if it is followed by a number in the "Weeks" field, it means "the last one weeks of the month X"-such as "6L" for "The last Friday of the month". When using the ' L ' option, it is important to specify the list or scope of the determination, otherwise you will be confused by the result.

The ' W ' is available for the day field. Used to specify the most recent weekday (Monday to Friday) for the given date of the calendar. For example, you set the "Day" field to "15W", meaning: "The most recent working day from the month of 15th." So if number 15th is Saturday, the trigger will be called at 14th, or Friday. If number 15th is Sunday, the trigger will be triggered at 16th, or Monday. If number 15th is Tuesday, it will be triggered the same day. However, if you set the "Day" field to "1W" and the number is Saturday, the trigger will be triggered in the next Monday, which is the 3rd number of the month, because it will not cross the range boundary of the value of the month. The ' W ' character can only be used when the value of the day field is a single day instead of a series of values.

' L ' and ' W ' can be combined for the ' Day ' field as ' LW ', meaning ' last working day of the month '.

The ' # ' character can be used in the "Weeks" field. The character denotes "week X of the month", such as "6#3", which represents the third Friday of the month (6 for Friday and the third for the month "#3"). Another example: "2#1" = the first Monday of the month and "4#5" = Fifth Wednesday of the month. Note If you specify "#5" that the month does not have a fifth "week X", the month is not triggered.

The ' C ' character can be used for the "Day" and "Week" fields, which are abbreviations for "calendar". It represents the value, if any, that is computed based on the associated calendar. If there is no associated calendar, it is equivalent to including all calendars. The day field value of 5C means "the first day of the calendar or after 5th," and the "Weeks" field value "1C" means "the first day of the calendar or after Sunday."

Valid characters are not case-sensitive for the month field and the weeks field.

Here are some complete examples:

Meaning of an expression

"0 0 12 * * ?" 每天中午十二点触发
"0 15 10 ? * *" 每天早上10:15触发
"0 15 10 * * ?" 每天早上10:15触发
"0 15 10 * * ? *" 每天早上10:15触发
"0 15 10 * * ? 2005" 2005年的每天早上10:15触发
"0 * 14 * * ?" Daily from 2 o'clock in the afternoon to 2:59 per minute trigger
"0 0/5 14 * * ?" Every 5 minutes from 2 o'clock in the afternoon to 2:55
"0 0/5 14,18 * * ?" Daily from 2 o'clock in the afternoon to 2:55 and from 6 to 6:55 every 5 minutes for two time periods
"0 0-5 14 * * ?" Daily 14:00 to 14:05 triggers per minute
"0 10,44 14 ? 3 WED" March of 14:10 and 14:44 triggers per Wednesday
"0 15 10 ? * MON-FRI" 10:15 triggers per Monday, Tuesday, Wednesday, Thursday, Friday
"0 15 10 15 * ?" 10:15 Trigger per month at 15th
"0 15 10 L * ?" 10:15 triggers on the last day of the month
"0 15 10 ? * 6L" 10:15 triggers for the last Friday of the month
"0 15 10 ? * 6L" 10:15 triggers for the last Friday of the month
"0 15 10 ? * 6L 2002-2005" 10:15 triggers for the last Friday of the month from 2002 to 2005
"0 15 10 ? * 6#3" 10:15 triggers for the third Friday of the month

The above examples are my real projects changed ... Tested: O (∩_∩) o hahaha ~

Hey.. The stomach starved to death .... Eat something to ... I haven't eaten at noon .... Hey, ~~~~~~~~.

"30 * * * *?" triggers the task every half minute

"30 10 * * *?" 10 minutes and 30 seconds per hour trigger task "30 10 1 * *?" 1 10 points 30 seconds per day trigger task "30, 10 1 *?" 20 monthly 20th points 1 seconds trigger task "10 30 30 10 1?" * "October 20 1:10 30 sec Trigger Task" 30 10 1 20 10? 2011 "October 20, 2011 1:10 30-Second trigger task" 30 10 1? 10 * 2011 "October 2011 1 points per day 10 minutes 30 seconds Trigger Task" 30 10 1? SUN 2011 "October 2011 every Sunday 1:10 30 seconds Trigger Task" 15,30,45 * * * *? "every 15 seconds, 30 seconds, 45 seconds Trigger Task" 15-45 * * * *? " 15-45 seconds, each second triggers the task "15/5 * * * *?" starts every 15 seconds per minute, triggers every 5 Seconds "15-30/5 * * * *?" starts firing from 15 seconds to 30 seconds per minute, triggering every 5 seconds "0 0/3 * * *?" No. 0 minutes per hour Starting at 0 seconds, triggering every three minutes "0 15 10?" * Mon-fri "Monday to Friday 10 points 15 minutes 0 seconds trigger task 0 L *?" The last day of every month 10 points 15 minutes 0 seconds trigger the task "0 LW *?" 10 points 15 seconds per month for the last working day "0 0 15" trigger task * 5L "10 points per month for the last Thursday, 15 minutes, 0 seconds. Trigger a task

"0 15 10?" * 5#3 "The third week of the month of Thursday 10 points 15 minutes 0 seconds trigger a task

Reproduced Quartz Job scheduling Framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.