Quartz Task Scheduling framework integrates spring usage

Source: Internet
Author: User

IntroductionQuartz is an open-source job scheduling framework written entirely by Java, which is actually the framework for starting a timed task. When used, you only need to create an implementation class for the Org.quartz.Job interface, implementing the only method: public void Execute (jobexecutioncontext context) throws Jobexecutionexception , add your logical task to the Execute () method. first, the basic concept(i), execution object job: Work tasks, what are you going to do? Trigger: Perform job task trigger conditions, what time to execute, how often to write a work task

(ii) Use of Crontrigger

Crontriggers is usually more useful than simpletrigger if you need to trigger tasks on a schedule like a calendar instead of firing at specific intervals like Simpletrigger.

  Cron Expressions--cron

    1. Seconds sec

2. Minutes minutes
3. Hours hours
4. Days of the Day-of-month month
5 month
6. Day-of-week Days of the week
7. Year (optional field) (optional field)

All of the values in the domain have a specific legal scope, the legal range of these values is quite obvious, for example: the legal value of the second and the domain is 0 to 59, the legal range of the hour is 0 to 23,day-of-month is worth legal where the range is 0 to 31, but you need to be aware of the different days in different months. The legal value for the month is 0 to 11. or use the string Jan,feb MAR, APR, May, June, JUL,, SEP, OCT, NOV and DEC to indicate. Days-of-week can be represented by 1 to 7来 (1= Sunday) or by the string sun, MON, TUE, Wed,thu, FRI, and SAT.

The wildcard character (' * ') can be used to represent "each" possible value in a domain. Therefore, the * in the month field represents each month, while the * in the Day-of-week field represents "Every day of the week".

The '/' character is used to denote the increment of the value, for example, if the minute field is placed in ' 0/15 ', it means "every 15 minutes, starting from 0", if you use ' 3/20 ' in the part of the domain, it means "every 20 minutes in the hour, starting from the 3rd minute" or the same form is ' 3,23,43 '.

0 0 12? * 3 Every Tuesday noon 12 o'clock Perform tasks
0 0 0 0 *? 1st 0 O'Clock each month perform tasks

Note: 4. The month Zhongtian and 6. Zhou Zhongtian can not be taken at the same time *, indicating any day when one with *, another use?

Second, Spring integration Quartz

Spring-managed beans in the Job cannot be injected and need to be customized jobfactory resolved in Scheduler. (i), create jobfactory to inject job to implement spring management
@Service ("Jobfactory")publicclassextends  adaptablejobfactory{    @ autowired    private  autowirecapablebeanfactory capablebeanfactory;    @Override    protectedthrows  Exception {        super. Createjobinstance (bundle);        Capablebeanfactory.autowirebean (jobinstance);         return jobinstance;}    }
(ii), configuration Applicationcontext.xml 1, open packet scanning (jobfactory, the service layer of the work task is located in the package)
<context:component-scan base-package = "Com.hehe.quartz.service"/>
2, work task Jobdetail configuration----(Jobdetailfactorybean)
class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean" >
The class file that injects the dispatch task job <property name= "Jobclass" value= "Cn.itcast.quartz.job.HelloJob"/></bean>

3, Trigger trigger configuration----(Simpletriggerfactorybean)

<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerFactoryBean" >
Inject dispatch Task Job
<property name= "Jobdetail" ref= "Hellojob"/>
Set how long after execution starts (0 seconds after execution)
<property name= "Startdelay" value= "0"/>
Set how often the loop executes once (1-minute loop 1 times)
<property name= "Repeatinterval" value= "60000"/>

<bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" >
<property name= "Jobdetail" ref= "Examplejob"/>
<property name= "cronexpression" value= "0 0 6 * *?" />

4, scheduling Task Scheduler configuration----(Schedulerfactorybean)

class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >//inject work tasks
<property name= "Jobfactory" ref= "Jobfactory"/>
Injection trigger <property name= "triggers" > <list> <ref bean= "Simpletrigger"/> </ list> </property> </bean>
Attached: 1, quartz 2.2.1 Maven coordinates:
        <dependency>            <groupId>org.quartz-scheduler</groupId>            <artifactid>quartz</ artifactid>            <version>2.2.1</version>        </dependency>        <dependency>            <groupId>org.quartz-scheduler</groupId>            <artifactId>quartz-jobs</artifactId>            <version>2.2.1</version>        </dependency>   

Quartz Task Scheduling framework integrates spring usage

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.