First, the characteristics of quartz
* According to the operation of the inheritance of the way to divide, mainly the following two kinds:
1. How the job class inherits the Org.springframework.scheduling.quartz.QuartzJobBean class
2. How the job class does not inherit the Org.springframework.scheduling.quartz.QuartzJobBean class
Note: The second kind of personal comparison is respected, because the job class in this way is still pojo.
* According to task scheduling trigger time to divide, mainly the following two kinds:
1. Trigger once every specified time, the corresponding scheduler is Org.springframework.scheduling.quartz.SimpleTriggerBean
2. Trigger once per specified time, the corresponding scheduler is Org.springframework.scheduling.quartz.CronTriggerBean
Note: Both of these triggering methods can be used in combination with two job inheritance methods.
The following is a brief demonstration of the use of quartz in spring.
Second, the job class inherits the Org.springframework.scheduling.quartz.QuartzJobBean class, and fires once per specified time
1. Writing Job Classes
Package Bean.jobdetailbean;
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 schedule after timeout time
public void setTimeout (int timeout) {
This.timeout = timeout;
}
/**
* Specific tasks to dispatch
*/
@Override
protected void Executeinternal (Jobexecutioncontext context)
Throws Jobexecutionexception {
System.out.println ("The way to inherit Quartzjobbean-Dispatch" + ++i + "in progress ...");
}
}
2. Configure the Job class
<!--Jobs using inherited Quartzjobbean--
<bean name= "Job1" class= "Org.springframework.scheduling.quartz.JobDetailBean" >
<property name= "Jobclass" value= "Bean.jobDetailBean.Job1"/>
<property name= "Jobdataasmap" >
<map>
<entry key= "Timeout" value= "0"/>
</map>
</property>
</bean>
3. Configure how job scheduling is triggered
<!--corresponds to the job relay Quartzjobbean class--
<bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" ref= "Job1"/>
<!--
Configuration instructions for "Cronexpression"
Special characters allowed for field allowed values
Seconds 0-59,-*/
Sub 0-59,-*/
Hours 0-23,-*/
Date 1-31,-*? /L W C
Month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*? /L C #
Year (optional) leave blank, 1970-2099,-*/
-Interval
* Wildcard Characters
? You don't want to set that field.
-
<!--0,10,20,30,40,50 seconds per minute--
<property name= "cronexpression" value= "0,10,20,30,40,50 * * * *?"/>
</bean>
4. Configure the Dispatch factory
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Triggers" >
<list>
<ref bean= "Crontrigger"/>
</list>
</property>
</bean>
5. turn on scheduling
Package test;
Import Org.springframework.beans.factory.BeanFactory;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Scheduletest {
public static void Main (string[] args) {
Beanfactory factory = new Classpathxmlapplicationcontext ("Applicationcontext*.xml");
}
}
6. Results of schedule Execution
How to inherit Quartzjobbean-Schedule 1 in progress ...
How to inherit Quartzjobbean-Schedule 2 in progress ...
How to inherit Quartzjobbean-Schedule 3 in progress ...
How to inherit Quartzjobbean-Schedule 4 in progress ...
How to inherit Quartzjobbean-Schedule 5 in progress ...
How to inherit Quartzjobbean-schedule 6 in progress ...
How to inherit Quartzjobbean-Schedule 7 in progress ...
How to inherit Quartzjobbean-Schedule 8 in progress ...
How to inherit Quartzjobbean-Schedule 9 in progress ...
Third, the job class does not inherit the Org.springframework.scheduling.quartz.QuartzJobBean class, every specified time is triggered once
1. Writing Job Classes
Package Bean.jobdetailbean;
public class Job2 {
private static int i = 0;
public void DoJob2 () {
System.out.println ("Do not inherit Quartzjobbean mode-Schedule" + ++i + "in progress ...");
}
}
2. Configure the Job class
<bean id= "Job2"
class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<property name= "TargetObject" >
<bean class= "Bean.jobDetailBean.Job2"/>
</property>
<property name= "Targetmethod" value= "DoJob2"/>
<property name= "Concurrent" value= "false"/><!--job not concurrently dispatched--
</bean>
3. Configure how job scheduling is triggered
<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 execute the schedule--
<property name= "Repeatinterval" value= "a"/><!--dispatched every 2 seconds--
</bean>
4. Configure the Dispatch factory
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Triggers" >
<list>
<ref bean= "Simpletrigger"/>
</list>
</property>
</bean>
5. turn on scheduling
Package test;
Import Org.springframework.beans.factory.BeanFactory;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Scheduletest {
public static void Main (string[] args) {
Beanfactory factory = new Classpathxmlapplicationcontext ("Applicationcontext*.xml");
}
}
6. Results of schedule Execution
Do not inherit Quartzjobbean mode-Schedule 1 in progress ...
Do not inherit Quartzjobbean mode-Schedule 2 in progress ...
Do not inherit Quartzjobbean mode-Schedule 3 in progress ...
Do not inherit Quartzjobbean mode-Schedule 4 in progress ...
Do not inherit Quartzjobbean mode-Schedule 5 in progress ...
Do not inherit Quartzjobbean mode-Schedule 6 in progress ...
Do not inherit Quartzjobbean mode-Schedule 7 in progress ...
Do not inherit Quartzjobbean mode-Schedule 8 in progress ...
Do not inherit Quartzjobbean mode-Schedule 9 in progress ...
Do not inherit Quartzjobbean mode-Schedule 10 in progress ...
Use of the quartz scheduler in spring