Using Opensymphony Quartz Scheduler in SPRINGMVC

Source: Internet
Author: User

Yesterday, I received a copy of the test to be used to the scheduler today began to get a bit of the previous project has been used, but temporary learning is not good to use today's time to find a lot of places do not know

The result of the old error when it was started is because the package was introduced less.

Quartz-all-1.6.0.jar

Spring-context-support.jar

Commons-collections-3.2.jar Be sure if the version of 3.x default SSH is 2. Version of X

I feel so good, because of this mistake, I went to find the great God.

Say code

Quartz use Trigger , Job and JobDetail other objects to perform various types of task scheduling. For basic concepts on quartz, see Http://www.opensymphony.com/quartz. To make spring-based applications easy to use, Spring provides classes to simplify the usage of Uartz.

23.2.1. Using Jobdetailbean

JobDetailObject saves all the information that is required to run a task. Spring provides a named JobDetailBean class that enables JobDetail initialization of some meaningful initial values. Let's look at an example:

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

The job detail Bean has all ExampleJob the necessary information to run the job (). Timeout can be set up through the job's data map. The data map of the job can be JobExecutionContext obtained by passing it to you at run time, but JobDetailBean at the same time mapping the properties obtained from the job's data map to the attributes in the actual job. Therefore, if Examplejob contains a timeout property named, JobDetailBean it will be assigned a value automatically:

Package Example;public class Examplejob extends Quartzjobbean {  private int timeout;    /**   * Setter called after the examplejob are instantiated * with the value from the   Jobdetailbean (5)   */   PU Blic void setTimeout (int timeout) {    this.timeout = timeout;  }    protected void Executeinternal (Jobexecutioncontext ctx) throws jobexecutionexception {      //Do the actual work
      }}

Of course, you can also set up all the other additional configurations in the job detail bean.

Note: Using name and group attributes, you can individually modify which group the job runs under and what name to use. By default, the job name equals the name of the job detail bean (in the example above exampleJob ).

23.2.2. Using MethodInvokingJobDetailFactoryBean

In general, you only need to invoke a method on a specific object to implement task scheduling. You can use the MethodInvokingJobDetailFactoryBean exact to do this:

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

The above example will be called exampleBusinessObject in the doIt method (below):

public class Examplebusinessobject {    //properties and collaboratorspublic    void DoIt () {    //Do The actualwork  }}
<bean id= "Examplebusinessobject" class= "examples. Examplebusinessobject "/>

With MethodInvokingJobDetailFactoryBean a job where you don't need to create a single line of code and call only one method, you just need to create real business objects to wrap the specific details of the object.

By default, Quartz jobs is stateless and can lead to a mutual effect between jobs. If you JobDetail specify two trigger for the same, it is likely that the second job will begin before the first job is completed. If an JobDetail object implements an Stateful interface, such a thing will not happen. The second job will not start until the first job is completed. In order for jobs not to run concurrently, the MethodInvokingJobDetailFactoryBean markup in the settings is concurrent false .

<bean id= "Jobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >  <property name= "TargetObject" ref= "Examplebusinessobject"/>  <property name= "Targetmethod" value= "DoIt"/>  <property name= "concurrent" value= "false"/></bean>
Attention

Note: By default, Jobs runs in a parallel manner.

23.2.3. Using triggers and SchedulerFactoryBeanTo package the task

We have created the job details,jobs. We also reviewed the handy bean that allows you to invoke a method on a particular object. Of course we still need to dispatch these jobs. This needs to be done using triggers and SchedulerFactoryBean . Quartz comes with some triggers that are available for use. Spring provides two subclass triggers, respectively, CronTriggerBean and SimpleTriggerBean .

Triggers also needs to be dispatched. Spring provides SchedulerFactoryBean to expose some properties to set triggers. SchedulerFactoryBeanresponsible for dispatching those actual triggers.

Take a look at some examples:

<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" >    <!--see The example of method invoking job above--    <property name= "Jobdetail" ref= "Jobdetail"/>    <!--sec Onds--    <property name= "Startdelay" value= "10000"/>    <!--Repeat every-seconds-    < Property Name= "Repeatinterval" value= "50000"/></bean><bean id= "Crontrigger" class= " Org.springframework.scheduling.quartz.CronTriggerBean ">    <property name=" Jobdetail "ref=" Examplejob "/ >    <!--run every morning at 6 AM-and    <property name= "cronexpression" value= "0 0 6 * *?"/></bea N>

Now we have created two triggers, one of which starts to run every 50 seconds after 10 seconds of delay and the other runs every 6 o'clock in the morning. We need to create one SchedulerFactoryBean that will ultimately accomplish all of the above:

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


Using Opensymphony Quartz Scheduler in SPRINGMVC

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.