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
123456789101112131415161718192021 |
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
;
//调度工厂实例化后,经过timeout时间开始执行调度
public
void
setTimeout(
int
timeout) {
this
.timeout = timeout;
}
/**
* 要调度的具体任务
*/
@Override
protected
void
executeInternal(JobExecutionContext context)
throws JobExecutionException {
System.out.println(
"定时任务执行中…"
);
}
}
|
Step Two: Configure the job class in the spring configuration file Jobdetailbean
12345678 |
<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:
12345 |
<bean id=
"simpleTrigger"
class
=
"org.springframework.scheduling.quartz.SimpleTriggerBean"
>
<property name=
"jobDetail"
ref=
"job1"
/>
<property name=
"startDelay"
value=
"0" /><!– 调度工厂实例化后,经过
0
秒开始执行调度 –>
<property name=
"repeatInterval"
value=
"2000"
/><!– 每
2
秒调度一次 –>
</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:
12345 |
<bean id=
"cronTrigger" class
=
"org.springframework.scheduling.quartz.CronTriggerBean"
>
<property name=
"jobDetail"
ref=
"job1"
/>
<!—每天
12
:
00
运行一次 —>
<property name=
"cronExpression" value=
"0 0 12 * * ?"
/>
</bean>
|
Fourth Step: Configure the Dispatch factory
1234567 |
<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
12345 |
public class Job2 { public void doJob2() { System.out.println( "不继承QuartzJobBean方式-调度进行中…" ); } } |
Note: As you can see, this is an ordinary class, and there is a method.
Step Two: Configure the Job class
12345678 |
<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"
/><!– 作业不并发调度 –>
</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:
12345 |
<bean id=
"simpleTrigger"
class
=
"org.springframework.scheduling.quartz.SimpleTriggerBean"
>
<property name=
"jobDetail"
ref=
"job2"
/>
<property name=
"startDelay"
value=
"0"
/><!– 调度工厂实例化后,经过
0
秒开始执行调度 –>
<property name=
"repeatInterval"
value=
"2000"
/><!– 每
2
秒调度一次 –>
</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:
12345 |
ronTriggerBean"> <property name= "jobDetail" ref= "job2" /> <!—每天 12 : 00 运行一次 —> <property name= "cronExpression" value= "0 0 12 * * ?" /> </bean> |
The above two kinds of scheduling methods according to the actual situation, choose one can.
Fourth Step: Configure the Dispatch factory
1234567 |
<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!
Quote Original: http://www.cnblogs.com/hongwz/p/5642429.html
Blog is to remember that they are easy to forget things, but also a summary of their work, the article can be reproduced, without copyright. Hope to do their own efforts to do better, we work together to improve!
If there is any problem, welcome to discuss together, code if there is a problem, you are welcome to the great God!
The quartz of spring task scheduling