Quartz simple trigger for spring Job Scheduling

Source: Internet
Author: User

Address: http://blog.csdn.net/kongxx/article/details/6751300

In spring, in addition to using the built-in timer and timertask classes of JDK to implement simple task scheduling, you can also use the enterprise-level open-source job scheduling framework quartz, below is a small example:

1. First, it is a task class. This class does not implement any interfaces. It contains a run method used to run this task,CodeAs follows:

Package Org. garbagecan. springstudy. schedule. quartz; </P> <p> public class mytask {<br/> private string name; </P> <p> Public void run () {<br/> system. out. println ("Run task:" + name + ". "); <br/>}</P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/>}2. A job class that inherits the quartzjobbean class of spring to indicate that the current class is a job class of quartz. The class contains an object instance of a task class, when a job is scheduled, the executeinternal method is run. The Code is as follows:

Package Org. garbagecan. springstudy. schedule. quartz; </P> <p> Import Org. quartz. jobexecutioncontext; <br/> Import Org. quartz. jobexecutionexception; <br/> Import Org. springframework. scheduling. quartz. quartzjobbean; </P> <p> public class myjob extends quartzjobbean {</P> <p> private mytask; </P> <p> protected void executeinternal (jobexecutioncontext context) <br/> throws jobexecutionexception {</P> <p> mytask. run (); <br/>}</P> <p> Public mytask getmytask () {<br/> return mytask; <br/>}</P> <p> Public void setmytask (mytask) {<br/> This. mytask = mytask; <br/>}< br/>}3. The spring configuration file contains the following content:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" <br/> default-Lazy-init = "true"> </P> <p> <bean id = "timerfactory" class = "org. springframework. scheduling. quartz. schedulerfactorybean "lazy-init =" false "> <br/> <property name =" triggers "> <br/> <list> <br/> <ref local =" simpletrigger" /> <br/> </List> <br/> </property> <br/> </bean> </P> <p> <bean id = "simpletrigger" class = "org. springframework. scheduling. quartz. simpletriggerbean "> <br/> <property name =" jobdetail "ref =" myjob "/> <br/> <property name =" startdelay "value =" 0 "/> <br/> <property name = "repeatinterval" value = "10000"/> <br/> </bean> </P> <p> <bean id = "myjob" Class = "org. springframework. scheduling. quartz. jobdetailbean "> <br/> <property name =" name "value =" myjob "/> <br/> <property name =" group "value =" mygroup "/> <br/> <property name = "Description" value = "myjob"/> <br/> <property name = "jobclass" value = "org. garbagecan. springstudy. schedule. quartz. myjob "/> <br/> <property name =" jobdataasmap "> <br/> <map> <br/> <Entry key =" mytask "value-ref =" mytask "/> <br/> </map> <br/> </property> <br/> </bean> </P> <p> <bean id =" mytask "Class =" org. garbagecan. springstudy. schedule. quartz. mytask "> <br/> <property name =" name "value =" My task "/> <br/> </bean> </P> <p> </beans>3.1 first, a task class needs to be defined. The specific task logic can be written in this class;
3.2 define a bean of the jobdetailbean class. This is to adapt to the jobdetail of quartz. information such as name, group, and description can be defined. This information is mainly used to distinguish it from other jobs; there is also a jobclass attribute, which defines the job class instance to be used. Here, the myjob class created in step 2 is used. Finally, a property called jobdataasmap is defined, this attribute is a map set provided by quartz to pass Parameters. classes in these sets are automatically injected into the target job class by spring;
3.3 create a trigger class. Here we use the simple trigger provided by quartz. In spring, simpletriggerbean is used for ing, which mainly defines the delay time of the first running, the interval between repeated executions. Of course, there are also the most important and specific job-class instances;
3.4 Finally, a schedulerfactorybean bean is defined, which defines the specific triggers to be scheduled;

4. Finally, write a test class to test the above Code and configuration.

Package Org. garbagecan. springstudy. schedule. quartz; </P> <p> Import Org. springframework. context. support. classpathxmlapplicationcontext; </P> <p> public class test {<br/> Public static void main (string [] ARGs) throws exception {<br/> New classpathxmlapplicationcontext ("/org/garbagecan/springstudy/schedule/quartz/spring. XML "); <br/>}< br/>}Run the test class. You can see that a job named my task starts and runs every 10 seconds.

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.