Spring Quartz Task Scheduler

Source: Internet
Author: User

UseQuartzScheduler

Quart uses Trigger, Job, and jobdetail objects to implement various scheduling tasks. For The basic concepts behind Quartz, refer to http://quartz-scheduler.org. For convenience purposes,Spring provides a set of classes to simplify the use of Quartz in spring -based applications.

UseJobdetailbean

the Jobdetail object contains all the information that needs to run the task. the Spring Framework provides a Jobdetailbean with a real JavaBean that uses more reasonable default values to produce jobdetail. Let's take a look at the following example:

<bean name= "Examplejob" class= "Org.springframework.scheduling.quartz.JobDetailBean" > <property name= "JOBC Lass "Value=" example. Examplejob "/> <property name=" Jobdataasmap "> <map> <entry key=" Timeout "value=" 5 " /> </map> </property></bean>

package example;    public class Examplejob extends Quartzjobbean {private int timeout; /** * Examplejob setter is called after instantiation from Jobdetailbean */public void setTimeout (int timeout) {THIS.T    Imeout = timeout; } protected void Executeinternal (Jobexecutioncontext ctx) throws Jobexecutionexception {//Do the actual Work}}

All the additional settings for Jobdetailbean are of course valid for you.

Using the name and group properties, you can modify the name and group of the task individually. By default, the name of the task matches the name of Jobdetailbean (in the example above, it is examplejob).

UseMethodinvokinjobdetailfactorybean

usually you need to call a method on the specified object. With Methodinvokingjobdetailfactorybean You can do this:

<bean id= "Jobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > < Property Name= "TargetObject" ref= "Examplebusinessobject"/> <property name= "Targetmethod" value= "DoIt"/>& Lt;/bean>

The above example will cause the examplebusinessobject doit method to be called:

public class Examplebusinessobject {//attribute and reference public void DoIt () {//Real business}}

<bean id= "Examplebusinessobject" class= "examples. Examplebusinessobject "/>

Use Methodinvokingjobdetailfactorybean, you don't need to create a single line of tasks to call a method immediately, you just need to create an actual business object and assemble it to the detail object.

By default, Quartz The task is stateless, which leads to the possibility that tasks interfere with each other. If you use the same jobdetail To specify two triggers, he may finish before the first task, and the second task will begin. This will not happen if jobdetail stateful interface. The second task will no longer start before the first task is completed. To make the task from methodinvokingjobdetailfactorybean Concurrent false

<bean id= "Jobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > &L T;property name= "TargetObject" ref= "Examplebusinessobject"/> <property name= "Targetmethod" value= "DoIt"/>        ; <property name= "Concurrent" value= "false"/></bean>

By default, the task runs in the form of concurrency.

use triggers andSchedulerfactorybeanAssembly Tasks

We will create task detail and tasks. We also review the convenience bean that allows you to invoke a method on the specified object . Of course, we will need to schedule the task itself. Use triggers and Schedulerfactorybean to complete. multiple triggers are valid in Quartz,Spring provides two Quartz Factorybean Default convenience implementations:Crontriggerfactorybean and Simpletriggerfactorybean.

triggers need to be dispatched. Spring provides a Schedulerfactorybean exposed trigger as a property setting. Schedulerfactorybean Use these triggers to schedule actual tasks.

Take a look at the following example:

<bean id= "Simpletrigger"  class= "Org.springframework.scheduling.quartz.SimpleTriggerFactoryBean" >        <!--Call the method in the example above  -->         <property name= "Jobdetail"  ref= "Jobdetail"  />         <!-- 10 S  -->        < Property name= "Startdelay"  value= "10000"  />         <!--50 seconds to repeat  -->        <property name= " Repeatinterval " value=" 50000 " /></bean> <bean id=" CronTrigger " class=" Org.springframework.scheduling.quartz.CronTriggerFactoryBean ">         <property name= "Jobdetail"  ref= "Examplejob"  />         <!--run &NBSP;--&G daily Six o'clock in the morningT;        <property name= "Cronexpression"  value= "0 0  6 * * ? "  /></bean>

now, we set up two triggers, one Ten seconds of start-up delay per - seconds to run once, another every morning 6 point to run once. To finalize one thing, we need to set schedulerfactorybean:

<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name= "triggers" > <list> <ref bean= "Crontrigger"/> <ref bean= "sim Pletrigger "/> </list> </property></bean>

Can you give Schedulerfactorybean Set more valid properties, such as using the calendar's task detail, using attributes to customize Quartz, and so on. More details refer to Schedulerfactorybean's JavaDoc.


Spring Quartz Task Scheduler

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.