Quartz job scheduling framework integrated with Spring, quartzspring

Source: Internet
Author: User

Quartz job scheduling framework integrated with Spring, quartzspring
IntroductionQuartz is an open-source job scheduling framework fully compiled by java. It is actually a framework for starting scheduled tasks. Only one org. quartz. the only way to implement the implementation class of the Job interface is public void execute (JobExecutionContext context) throws JobExecutionException. Add your logical task to the execute () method.I. Basic Concepts(1) Execution object Job: Job, what Trigger do you want to perform: the Trigger condition for executing a Job, the execution time, And the execution time of writing a Job

(2) Use of CronTrigger

CronTriggers is generally more useful than SimpleTrigger if you need to trigger a task by schedule like a calendar, rather than at a specified interval like SimpleTrigger.

  Cron Expressions -- Cron

    1. Seconds

2. Minutes
3. Hours hour
4. Days in Day-of-Month
5. Month
6. Day-of-Week Day of Week
7. Year (optional field)

Values in all fields have a specific valid range. The valid range of these values is quite obvious. For example, valid values of seconds and domain shards are 0 to 59, the valid range of hours is 0 to 23, and the value of Day-of-Month must be 0 to 31. However, you must note that the days in different months are different. The valid values of a month are 0 to 11. It can also be represented by the string JAN, feb mar, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, and DEC. Days-of-Week can be expressed by 1 to 7 (1 = Sunday) or SUN, MON, TUE, WED, THU, FRI, and SAT.

Wildcard ('*') can be used to indicate the possible values of "each" in the domain. Therefore, * in the "Month" field indicates each Month, and * in the Day-Of-Week field indicates "every Day Of the Week ".

The '/' character is used to represent the increment of the value. For example, if the minute domain is put into '123', it indicates "every 15 minutes, starting from 0 ", if '20140901' is used in the intermediate domain, it indicates "every 20 minutes in the hour, starting from 3/20 minutes" or the same format is '3, 3rd '.

0 0 12? * 3 execute tasks at every Tuesday
0 0 0 *? Task execution at on the first day of each month

Note: 4. Days in the middle and 6. Days in the middle of the week cannot get * at the same time, indicating one use * at any day and another use?

 Ii. Spring IntegrationQuartz

The Bean managed by spring in the Job cannot be injected. You need to customize JobFactory in schedfactory. (1) Create JobFactory for injecting jobs to implement Spring Management
@Service("jobFactory")public class JobFactory extends AdaptableJobFactory{    @Autowired    private AutowireCapableBeanFactory capableBeanFactory;    @Override    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {        Object jobInstance = super.createJobInstance(bundle);        capableBeanFactory.autowireBean(jobInstance);        return jobInstance;    }}
(2) Configure applicationContext. xml 1. enable packet scanning (the package of JobFactory and the Service layer of the job)
<context:component-scan base-package="com.hehe.quartz.service" />
2. Job JobDetail configuration ---- (JobDetailFactoryBean)
<Bean id = "helloJob" class = "org. springframework. scheduling. quartz. JobDetailFactoryBean">
// Inject the Class file <property name = "jobClass" value = "cn. itcast. quartz. Job. HelloJob"/> </bean>

3. Trigger configuration ---- (SimpleTriggerFactoryBean)

// SimmpleTrigger
<Bean id = "simpleTrigger" class = "org. springframework. scheduling. quartz. SimpleTriggerFactoryBean">
// Inject a scheduling task job
<Property name = "jobDetail" ref = "helloJob"/>
// Set how long it will take to start execution (0 seconds later)
<Property name = "startDelay" value = "0"/>
// Set the duration of loop execution once (1 minute cycle once)
<Property name = "repeatInterval" value = "60000" type = "regxph" text = "yourobjectname"/>
</Bean>

// cronTrigger
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="exampleJob"/>
<property name="cronExpression" value="0 0 6 * * ?"/>
</bean>

4. Scheduler configuration ---- (SchedulerFactoryBean)

<Bean class = "org. springframework. scheduling. quartz. SchedulerFactoryBean"> // inject a job
<Property name = "jobFactory" ref = "jobFactory"/>
// Injection trigger <property name = "triggers"> <list> <ref bean = "simpleTrigger"/> </list> </property> </bean>
Appendix: 1. Maven coordinates of quartz 2.2.1:
        <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>    

Related Article

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.