Address: http://blog.csdn.net/kongxx/article/details/6751326
In addition to the simplest Simple Trigger in Quartz, you can run a Job in a way similar to CronTrigger of Cron jobs in Linux. 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. The Code is as 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 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 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> <bean id = "timerFactory" class = "org. springframework. scheduling. quartz. schedulerFactoryBean "lazy-init =" False "> <br/> <property name =" triggers "> <br/> <list> <br/> <ref local =" "cronTrigger" "/> <br/> </list> <br/> </property> <br/> </bean> </p> <bean id = "cronTrigger" class = "org. springframework. scheduling. quartz. cronTriggerBean "> <br/> <property name =" jobDetail "ref =" myJob "/> <br/> <property name =" cronExpression "value =" 0/10 ****? "> </Property> <br/> </bean> </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> <bean id =" myTask "class =" org. garbagecan. springstudy. schedule. quartz. myTask "> <br/> <property name =" name "value =" my task "/> <br/> </bean> </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 the Cron Trigger provided by Quartz is used. In Spring, CronTriggerBean is used for ing, mainly to define cron expressions, for details, refer to the official documentation of quartz, which defines running every 10 seconds. 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.