Integrated use of quartz and spring

Source: Internet
Author: User

Said before about the basic use of quartz (poke here to see the article). In practical use, we typically refer to the spring container to manage the scheduled tasks. So today we're talking about the integration of quartz and spring.

Let's talk about the quartz in the order of the three main elements.

Job Task

In spring, there are two main ways to manage job tasks for quartz, Jobdetailfactorybean and Methodinvokingjobdetailfactorybean, They're all org.springframework.scheduling.quartz under this bag. Let's take a look at the use of them below.

    • Jobdetailfactorybean

Spring's interpretation of this class is: A spring factorybean for creating a Quartz Jobdetail instance, supporting Bean-style usage for Jobdetail Co Nfiguration.
One to create the Quartz jobdetail instance. Factory beans that support the configuration of jobdetail in bean-defined style.

The

is also very easy to use in spring, first we need to create a detailed implementation class for the job task. When using Jobdetailfactorybean to manage job tasks, our job task implementation class needs to inherit the Quartzjobbean class and overwrite its Executeinternal method. Just like the following.

public   class  simplejob  extends  quartzjobbean  {  @Override  protected  void  executeinternal  (jobexecutioncontext arg0) throws  jobexecutionexception {System.out.println ( "Now Time is:"         +new  Date ());        //is able to get to jobdatamap through the context, which can hold data of some parameter types         Jobdatamap Datamap=arg0.getmergedjobdatamap ();        String wish= (String) datamap.get ( "wish" );    System.out.println (Wish); }}

Then, in the spring container, for example, the following configuration:

<bean id= "jobdetailfactorybeanexample" class=" Org.springframework.scheduling.quartz.JobDetailFactoryBean ">    <!--reference source code. We can see that the attribute Jobclass is the class type. Therefore, you cannot use ref to reference a bean.        Otherwise, an exception occurs because the bean cannot be converted to the class type.         <property name= "Jobclass" ref= "Simplejob"/> must use value to assign a value to Jobclass. <property name= "Jobclass" value= "Com.earl.quartz.spring.job.SimpleJob"/> --    < property name= "jobclass" value=" Com.earl.quartz.spring.job.SimpleJob "/>    <!--The Jobdataasmap set here can pass some of the parameters to the job task--    < property name="Jobdataasmap">        <map>            <entry key="Wish" value="Hello"/>        </map>    </Property ></Bean>
    • Methodinvokingjobdetailfactorybean

Spring's interpretation of this class: Factorybean, exposes a Jobdetail object which delegates job execution to a specified (static or Non-stat IC) method.
This factorybean provides a Jobdetail object that can specify how the job task will run.

It is more flexible and convenient to use than Jobdetailfactorybean because it can specify what to run when the job is scheduled. Let's start by creating a detailed implementation class for the job task, which does not have to inherit or implement the other parent classes, just declare the job task we want to run in a detailed way. For example, the following:

publicclass ExampleJob{    publicvoidexecute(){        System.out.println("如今是"+new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date()));    }}

Then in the spring container, for example, the following configuration is possible:

<!-- 假设两个触发器触发同一个作业,那么第二个作业可能在第一个作业完毕之前被触发。 将作业类实现StatefulJob接口就能够避免这样的情况。 将concurrent设置为false能够避免并发的发生。

-<!--use Methodinvokingjobdetailfactorybean to create a Job object --<bean id= "examplejob" class=" Com.earl.quartz.spring.job.ExampleJob "/><bean id="Methodinvokingjobdetailfactorybeanexample" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!--target object, which is the implementation class of the job task- -< property name="TargetObject" ref="Examplejob"/><!--target method, which is the method of specifying which method in the implementation class to run as a dispatch.< property name="Targetmethod" value="Execute"/> < Whether!--is concurrent --< property name="Concurrent" value="false"/></Bean>

The above is the job task related content, the following we look at the trigger this cute little stuff.

Trigger

In spring, triggers are also divided into Simpletrigger and Crontrigger. And they are very easy to use, just need to configure a bean element. Let's look at the configuration of both of them separately:

    • Simpletrigger
<bean id= "Simpletrigger" class=" Org.springframework.scheduling.quartz.SimpleTriggerFactoryBean ">    <!--here Jobdetail refers to the bean that we configured for the job task- -    < property name= "jobdetail" ref=" Methodinvokingjobdetailfactorybeanexample " />    <!--delay starts 5 seconds --    <property Name="Startdelay" value=" All" ></Property >    <!--repeats once every 3 seconds --       <property Name="Repeatinterval" value=" +" ></Property >     </Bean>
    • Crontrigger
<bean id= "Crontrigger" class=" Org.springframework.scheduling.quartz.CronTriggerFactoryBean ">    <!--here Jobdetail refers to the bean that we configured for the job task- -    < property name="Jobdetail" ref="Jobdetailfactorybeanexample" />    <!--cronexpression,cron Expressions--    < property name="Cronexpression" value="max. * *?" /></Bean>

The above is the basic configuration of the trigger. The factory bean of the above two triggers other properties, such as jobdatamap,priority and so on. If there is a need, you can refer to the relevant documentation.

Dispatch program

Finally, the simplest is the job scheduler, which only needs to be configured in spring:

<bean id= "Startquartz" class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean " lazy-init=" false ">    <!--specify which triggers to use. Spring will be dispatched to trigger the corresponding trigger. The job task is then scheduled and processed --    < property name="triggers">        <list><!--<ref bean= "Simpletrigger"/> --            <ref Bean="Crontrigger"/>        </list>    </Property ></Bean>
Summarize

The above is a basic introduction to the integration of quartz and spring.

On the whole, Quartz's timing task function is already very powerful, and spring's integration of it is to let the program ape in the use of timed tasks is a power. There are many other functions for quartz, such as timed file scanning. Send emails regularly, and so on, and then in a separate article in detail.

Poke here Download source code
Description: This article describes the integration of quartz and spring use, so please refer to the source code to focus on the contents of the Com.earl.quartz.spring package can be, others can be ignored by themselves.

Integrated use of quartz and spring

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.