The task scheduling framework quartz The high-end of the five-week operation

Source: Internet
Author: User

Usually we are using the JDK inside the timer trigger, but he has a big drawback, is to set the frequency of execution, which for processing a large number of requests for the server is a relatively resource-intensive thing, because I deal with a large number of requests is already busy enough, and then to check the task efficiency is low.

Quartz has two major triggers, except for the Simpletrigger used above, which is crontrigger. One is a simple trigger, one is a task trigger, the latter can be configured detailed task execution time, accurate to a certain year, a month, a day, a certain time, a second, when we do the project is more commonly used.

Timer

The timer inside the JDK is relatively simple, mainly setting the execution frequency and its delay time.

Quartz:

Simpletrigger and Crontrigger, first of all, his configuration is described below: see the relevant notes for the configuration file

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"4xmlns:context= "Http://www.springframework.org/schema/context"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx"6xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd7http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd 8http//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd9http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">Ten      One<!--register a regular bean-- A<bean id= "Quartztask"class= "Cn.hp.nsfw.complain.quartz.QuartzTask" ></bean> -<!--set up task information-- -<bean id= "JodDetail1"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > the<!--Setting the execution object-- -<property name= "TargetObject" ref= "Quartztask" ></property> -<!--setting methods in the execution object-- -<property name= "Targetmethod" value= "Dosimpletriggertask" ></property> +<!--settings are synchronized (synchronization can be performed at the same time with more than 2 tasks) by default it can be executed concurrently-- -<property name= "Concurrent" value= "false" ></property> +</bean> A      at<bean id= "JodDetail2"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > -<!--Setting the execution object-- -<property name= "TargetObject" ref= "Quartztask" ></property> -<!--setting methods in the execution object-- -<property name= "Targetmethod" value= "Docrontriggertask" ></property> -<!--settings are synchronized (synchronization can be performed at the same time with more than 2 tasks) by default it can be executed concurrently-- in<property name= "Concurrent" value= "false" ></property> -</bean> to      +<!--make a task execution time (that is, a trigger for a task)-- -<bean id= "Simpletrigger"class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" > the<!--set Task details-- *<property name= "Jobdetail" ref= "JodDetail1" ></property> $<!--set task delay execution time, two seconds delayPanax Notoginseng<property name= "Startdelay" value= "></property>" -<!--set the frequency of task execution at 2 seconds-- the<property name= "Repeatinterval" value= "></property>" +</bean> A      the<!--task Trigger-- +<bean id= "Crontrigger"class= "Org.springframework.scheduling.quartz.CronTriggerBean" > -<!--set Task details-- $<property name= "Jobdetail" ref= "JodDetail2" ></property> $<!--set task delay execution time-- -<property name= "cronexpression" value= "0/3 * * * *?" ></property> -</bean> the      -<!--3. Set up the dispatch factory--Wuyi<beanclass= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > the<!--set up triggers-- -<property name= "Triggers" > Wu<list> -<!--<ref bean= "Simpletrigger"/>-- About<!--<ref bean= "Crontrigger"/>-- $</list> -</property> -</bean> -      A</beans>
View Code

Related Bean Classes

1  PackageCn.hp.nsfw.complain.quartz;2 3 ImportJava.text.SimpleDateFormat;4 Importjava.util.Date;5 6  Public classQuartztask {7     8      Public voidDosimpletriggertask ()9     {TenSystem.out.println ("Dosimpletriggertask ... "+New  OneSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); A     } -      -      Public voidDocrontriggertask () the     { -System.out.println ("Docrontriggertask ... "+New  -SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); -     } +  -}
View Code

The corresponding results:

Task Trigger:

For related configurations See the following instructions:

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

‘?‘ characters can be used in the days and weeks 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 "Hours" field to " ten points to Point ".

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".

‘/‘character used to specify the increment of a value. For example, in the seconds field, set to"0/15"represents"Section0 ,and the $seconds". and"5/15"it means"Section5 , A,and the".in the'/'Front plus"*"the character corresponds to the specified0seconds to start. each field has a series of values that can start or end. For the seconds and minutes fields, the value range is0to the -, for the hour field it is0to theatFor the day field, the0to the,For the Month field , the1to the A. "/"fields only help you to start from within the range of allowed values"SectionN "the value. So for the month field,"7/6"just means7month is opened instead of "every six months", Please note the subtle differences.

LThe characters can be used in both "Day" and "Week" fields. It is"Last"the abbreviation, However, there are different meanings in the two fields. For example,in the day field"L"represents"the last day of one months" --For January is toThe number for February is -(non-leap year). In the days of the week field, It's simply a representation"7" or "SAT", but is followed by a number if used in the weeks field, it represents"last one weeks of the month x" --such as"6L"represents"last Friday of the month". When using' L 'option is,It is important to specify the list or scope of the determination, otherwise you will be confused by the result.

Wavailable for the day field. Used to specify the most recent day of the calendar given date(Monday to Friday) . For example, you set the day field to"15W", meaning to: "from this month theThe most recent weekday". So if theIs Saturday, the trigger will be -The number is called in Friday. If thenumber is Sunday, the trigger will be -The number was triggered in Monday. If thenumber is Tuesday,then it will be triggered the same day. However, if you set the day field to"1W",and number one is Saturday ., the trigger will be in the next Monday, the month.3Number Trigger,because it does not cross the range bounds of the value of the month. ' W 'characters 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 "#3" for the third month. ). 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 is "5C" for " first day of calendar or number 5 and later ", " The weeks "field value of " 1C " means " the first day of the calendar or Sunday and beyond . "

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

The task scheduling framework quartz The high-end of the five-week operation

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.