-quartz Scheduler for Spring timed tasks

Source: Internet
Author: User

Company Project There is a scheduled task, has been trying to find out its flow, thought it is difficult, today there is time to study a bit, found in fact the implementation is very simple.

The data found that there are many kinds of timing tasks in spring, and there are two kinds of quartz scheduler, that is, the job class inherits from the specific base class:Org.springframework.scheduling.quartz.QuartzJobBean.

The other is that the job class does not need to inherit a particular base class. I think the second is more simple and concise, the company project is also used in this method. Now let's talk about the specific steps to write:

First step: Writing task classes

Java Code   

Package Com.grx.pay.quartz;

    /**
* Timed task test class
* < function detail Description >
    *
* @author Sun
* @version [version number, April 26, 2017]
    */
Public class Quarts_test
    {
Public Void Run ()
    {
System.out.println ("---------------------------------timed Task Test------------------------------------");
    }
    }

Step Two: Configure the Job class

<bean id= "Test_payordertask" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<property name= "TargetObject" ref= "Test_payordermanager" ></property><!--associated Beans--
<property name= "Targetmethod" value= "Run" ></property><!--execution Method--
<property name= "Concurrent" value= "false"/>
</bean>

Corresponds to the method address in the first step

<bean id= "Test_payordermanager" class= "Com.grx.pay.quartz.quarts_test"/>

Note: This step is a critical step, declaring a Methodinvokingjobdetailfactorybean with two key properties:targetobject Specifies the task class,Targetmethod specifies the method to run.

Step three: Configure trigger mode for job scheduling (triggers)

<!--1-minute execution--
<bean id= "Test_payordertrigger" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" >
<property name= "Jobdetail" ref= "Test_payordertask"/>
<property name= "cronexpression" value= "0 0/1 * * *?"/>
</bean>

Fourth Step: Configure the Dispatch factory  

<!--each defined task is referenced here to run-
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Triggers" >
<list>
<ref bean= "Test_payordertrigger"/>
</list>
</property>
</bean>

So a timed mission is done.

The following is a complete spring-quartz-test.xml content, note spring-quartz-test.xml to be introduced in the Springmvc file (commonly introduced method); <?xml version= "1.0" encoding= "UTF-8"?>

<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:util= "Http://www.springframework.org/schema/util"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xsi:schemalocation= "
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
Http://www.springframework.org/schema/util
Http://www.springframework.org/schema/util/spring-util-3.0.xsd ">

<!--each defined task is referenced here to run-
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Triggers" >
<list>
<ref bean= "Test_payordertrigger"/>
</list>
</property>
</bean>

<bean id= "Test_payordermanager" class= "Com.grx.pay.quartz.quarts_test"/>

<bean id= "Test_payordertask" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<property name= "TargetObject" ref= "Test_payordermanager" ></property><!--associated Beans--
<property name= "Targetmethod" value= "Run" ></property><!--execution Method--
<property name= "Concurrent" value= "false"/>
</bean>

<!--1-minute execution--
<bean id= "Test_payordertrigger" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" >
<property name= "Jobdetail" ref= "Test_payordertask"/>
<property name= "cronexpression" value= "0 0/1 * * *?"/>
</bean>

</beans>

Appendix:

Cronexpression configuration instructions, specific use and parameters please Baidu Google

Special characters allowed for field allowed values

Seconds 0-59,-*/

Sub 0-59,-*/

Hours 0-23,-*/

Date 1-31,-*? /L W C

Month 1-12 or JAN-DEC,-*/

Week 1-7 or Sun-sat,-*? /L C #

Year (optional) leave blank, 1970-2099,-*/

-Interval

* Wildcard Characters

? You don't want to set that field.

Here are just a few examples.

Cron expression meaning

"0 0 12 * *?" Triggered 12 o'clock noon every day

"0 15 10?" * * "trigger 10:15 every day"

"0 15 10 * *?" Triggered 10:15 daily

"0 15 10 * *?" * "10:15 per day" trigger

"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger

"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

-quartz Scheduler for Spring timed tasks

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.