First, the Quartz operation class inherits the way, can divide into two kinds:
- Job classes need to inherit from a particular job class base class. The need to inherit from the Org.springframework.scheduling.quartz.quartzjobbean;java.util.timer in quartz is required to inherit from Java.util.TimerTask.
- The job class is a normal Java class and does not need to inherit from any base class.
Note: The second approach is recommended because the classes are generic and do not need to be treated differently.
- From the trigger time of task scheduling, here is mainly for the job use of the trigger, mainly the following two kinds:
- Fires once every specified time, and the corresponding trigger in quartz is: Org.springframework.scheduling.quartz.SimpleTriggerBean
- Each time it is specified, it is triggered once, and the corresponding scheduler in Quartz is: Org.springframework.scheduling.quartz.CronTriggerBean
Note: These two triggers are not available for each task, such as the Java.util.TimerTask task, which can only be used for the first type. Both quartz and spring tasks can support both of these triggering conditions.
Second, the first kind, the job class inherits from the specific base class: Org.springframework.scheduling.quartz.QuartzJobBean
First step: Define the Job class
Import Org.quartz.JobExecutionContext; Import org.quartz.JobExecutionException; Import Org.springframework.scheduling.quartz.QuartzJobBean; public class Job1 extends Quartzjobbean { private int timeout; private static int i = 0; After scheduling the factory instantiation, execute the dispatch public void setTimeout (int timeout) { this.timeout = timeout after timeout time; } /** * Specific tasks to dispatch * /@Override protected void executeinternal (Jobexecutioncontext context) throws jobexecutionexception { System.out.println ("Timed Task Execution ...");} }
Step Two: Configure the job class in the spring configuration file Jobdetailbean
<bean name= "Job1" class= "Org.springframework.scheduling.quartz.JobDetailBean" > <property name= " Jobclass "value=" Com.gy.Job1 "/> <property name=" Jobdataasmap "> <map> <entry key= "Timeout" value= "0"/> </map> </property> </bean>
Description: Org.springframework.scheduling.quartz.JobDetailBean has two properties, the Jobclass property is the task class that we define in Java code, and the Jobdataasmap property is the attribute value that needs to be injected into the task class.
Step three: Configure trigger mode for job scheduling (triggers)
There are two types of job triggers for quartz, namely
Org.springframework.scheduling.quartz.SimpleTriggerBean
Org.springframework.scheduling.quartz.CronTriggerBean
The first type of Simpletriggerbean, which only supports calling tasks at a certain frequency, such as running every 30 minutes. The configuration is as follows:
<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" > <property Name= "Jobdetail" ref= "Job1"/> <property name= "startdelay" value= "0"/><!– scheduling factory instantiation, after 0 seconds to start scheduling –> <property name= "Repeatinterval" value= "$"/><!– dispatch once every 2 seconds –> </bean>
The second type of Crontriggerbean, which is supported to run once at a specified time, such as 12:00 per day. The configuration is as follows:
<bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" > <property Name = "Jobdetail" ref= "Job1"/> <!-Run once a day 12:00 <property name= "cronexpression" value= "0 0 * *?"/&G t; </bean>
Fourth Step: Configure the Dispatch factory
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name= "triggers" > <list> <ref bean= "Crontrigger"/> </list> </property> </bean >
Description: This parameter specifies the name of the trigger that was previously configured.
Fifth step: Launch your app and deploy the project to Tomcat or other containers.
Third, the job class does not inherit a particular base class.
Spring can support this approach thanks to two classes:
Org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean
Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
These two classes correspond to the two ways in which spring supports the task scheduling, that is, the timer task mode and the Quartz method that the Java comes from earlier in this article. Here I only write Methodinvokingjobdetailfactorybean usage, the advantage of using this class is that our task class no longer needs to inherit from any class, but the normal pojo.
First step: Writing task classes
public class Job2 {public void DoJob2 () { System.out.println ("Do not inherit Quartzjobbean mode-Dispatch in progress ...");} }
Note: As you can see, this is an ordinary class, and there is a method.
Step Two: Configure the Job class
<bean id= "Job2" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > <property name= "TargetObject" > <bean class= "com.gy.Job2"/> </property> <property name= "Targetmethod" value= "DoJob2"/> <property name= "concurrent" value= "false"/><!– Job not concurrent scheduling –> </bean>
Note: This step is a critical step, declaring a Methodinvokingjobdetailfactorybean with two key properties: targetobject Specifies the task class, Targetmethod specifies the method to run. The next step is the same as the method one, and for completeness, it is also posted.
Step three: Configure trigger mode for job scheduling (triggers)
There are two types of job triggers for quartz, namely
Org.springframework.scheduling.quartz.SimpleTriggerBean
Org.springframework.scheduling.quartz.CronTriggerBean
The first type of Simpletriggerbean, which only supports calling tasks at a certain frequency, such as running every 30 minutes. The configuration is as follows:
<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" > <property Name= "Jobdetail" ref= "Job2"/> <property name= "startdelay" value= "0"/><!– scheduling factory instantiation, after 0 seconds to start scheduling –> <property name= "Repeatinterval" value= "$"/><!– dispatch once every 2 seconds –> </bean>
The second type of Crontriggerbean, which is supported to run once at a specified time, such as 12:00 per day. The configuration is as follows:
Rontriggerbean "> <property name=" Jobdetail "ref=" Job2 "/> <!-Run once every 12:00 < Property Name= "Cronexpression" value= "0 0 * *?"/> </bean>
The above two kinds of scheduling methods according to the actual situation, choose one can.
Fourth Step: Configure the Dispatch factory
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name= "triggers" > <list> <ref bean= "Crontrigger"/> </list> </property> </ Bean>
Description: This parameter specifies the name of the trigger that was previously configured.
Fifth step: Launch your app and deploy the project to Tomcat or other containers.
To this, spring in Quartz basic configuration is finished, of course, before using, to import the corresponding spring package and quartz package, these will not be said.
In fact, it can be seen that quartz configuration seems quite complex, there is no way, because Quartz is actually a heavyweight tool, if we just want to simply perform a few simple scheduled tasks, there is no simpler tool, there!
The quartz of spring task scheduling