Address: http://blog.csdn.net/kongxx/article/details/6860732
I wrote a fewArticleFor more information about spring's integrated job scheduling function, see
Timer for spring Task Scheduling Quartz simple trigger for spring Job Scheduling Quartz cron trigger for spring Task Scheduling
However, all these examples are examples of static Job Scheduling. Here, static jobs refer to job information and scheduling information that are written to the spring configuration file, however, many real applications require Dynamic Job Scheduling, such as dynamically adding or deleting jobs and dynamically setting job triggers. The following describes how to schedule dynamic jobs in spring.
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 = "scheduler" class = "org. springframework. scheduling. quartz. schedulerfactorybean "lazy-init =" false "> <br/> <property name =" triggers "> <br/> <list> <br/> </List> <br/> </property> <br/> </bean> </P> <p> </beans>Only one schedule class instance is configured in the configuration, and the triggers list is empty. In this way, no job is schedule when the schedule is started.
4. Finally, write a test class to test the above Code and configuration.
Package Org. garbagecan. springstudy. schedule. dynamic; </P> <p> Import Org. quartz. crontrigger; <br/> Import Org. quartz. jobdetail; <br/> Import Org. quartz. schedorg; <br/> Import Org. quartz. schedulerexception; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; </P> <p> Import Java. text. parseexception; </P> <p> public class te St {<br/> Public static void main (string [] ARGs) {<br/> applicationcontext CTX = new classpathxmlapplicationcontext ("/org/garbagecan/springstudy/schedule/dynamic/spring. XML "); <br/> scheduler = (schedctx) CTX. getbean ("scheduler"); </P> <p> system. out. println ("scheduling to run tasks. "); <br/> for (INT I = 0; I <5; I ++) {<br/> try {<br/> jobdetail = new jobdetail (); <br/> jobdetail. s Etname ("job _" + I); <br/> mytask = new mytask (); <br/> mytask. setname ("task _" + I); <br/> jobdetail. getjobdatamap (). put ("mytask", mytask); <br/> jobdetail. setjobclass (myjob. class); <br/> scheduler. addjob (jobdetail, true); </P> <p> crontrigger = new crontrigger ("cron _" + I, schedgger. default_group, jobdetail. getname (), scheduler. default_group); <br/> crontrigger. setcronexpression ("0/10 ** **? "); </P> <p> scheduler. schedulejob (crontrigger); <br/>} catch (parseexception e) {<br/> E. printstacktrace (); <br/>} catch (schedulerexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> try {<br/> thread. sleep (60*1000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> system. out. println ("un-scheduling to run tasks. "); <br/> for (INT I = 0; I <5; I ++) {<br/> try {<br/> scheduler. unschedulejob ("cron _" + I, scheduler. default_group); <br/>} catch (schedulerexception e) {<br/> E. printstacktrace (); <br/>}< br/>}4.1 The test class first creates five jobdetail and crontrigger. Each jobdetail corresponds to one of our myjob and mytask class instances. Each crontrigger is a Cron expression that runs once every 10 seconds. For specific APIs, refer to the official documentation of quartz;
4.2 For each created jobdetail and crontrigger, quartz schedgger is used for scheduling;
4.3 wait 60 seconds and let the jobs created above run several times. The message output is displayed in the background;
4.4 jobdetail and crontrigger created on UN-schedule. After this code segment, all jobs are stopped.