Details and examples of Quartz scheduler in Java Spring
- 2020-06-12 08:58:25
- OfStack
1. Features of Quartz
* According to the inheritance of the job class, there are mainly the following two types:
1. The working class inheritance org. springframework. scheduling. quartz. QuartzJobBean class
2. The working class not inherit org. springframework. scheduling. quartz. QuartzJobBean class
Note: I prefer the second one, because the assignment in this way is still POJO.
* According to the trigger time of task scheduling, there are mainly the following two types:
1. Every time is triggered, the corresponding scheduler to org springframework. scheduling. quartz. SimpleTriggerBean
2. Each to a specified time is triggered, the corresponding scheduler to org springframework. scheduling. quartz. CronTriggerBean
Note: Both of these triggers can be combined with both of the job inheritance methods.
The following is a brief demonstration of the use of Quartz in spring.
2. The working class inheritance org. springframework. scheduling. quartz. QuartzJobBean class, 1 each to the designated time is triggered
1. Write the job class
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;
// The dispatch factory is instantiated after passing through timeout The time begins to execute the schedule
public void setTimeout(int timeout) {
this.timeout = timeout;
}
/**
* The specific task to be scheduled
*/
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
System.out.println(" inheritance QuartzJobBean The way of - scheduling " + ++i + " ongoing ...");
}
}
2. Configure the job class
<!-- Job use inheritance QuartzJobBean The way of -->
<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 the triggering mode of job scheduling
<!-- Corresponds to job succession QuartzJobBean Kind of the way -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job1" />
<!--
"cronExpression" Configuration description of
field permitted Special characters allowed
seconds 0-59 , - * /
points 0-59 , - * /
hours 0-23 , - * /
The date of 1-31 , - * ? / L W C
in 1-12 or JAN-DEC , - * /
week 1-7 or SUN-SAT , - * ? / L C #
Year (optional) blank , 1970-2099 , - * /
- interval
* The wildcard
? You don't want to set that field
-->
<!-- The minute 0,10,20,30,40,50 Second scheduling 1 time -->
<property name="cronExpression" value="0,10,20,30,40,50 * * * * ?" />
</bean>
4. Configure the scheduling factory
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
5. Enable scheduling
package test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScheduleTest {
public static void main(String[] args){
<span style="white-space:pre"> </span>BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
}
}
3. The working class not inherit org. springframework. scheduling. quartz. QuartzJobBean class, every time is triggered once
1. Write the job class
package bean.jobDetailBean;
public class Job2 {
private static int i = 0;
public void doJob2() {
System.out.println(" No inheritance QuartzJobBean way - scheduling " + ++i + " ongoing ...");
}
}
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" /><!-- Jobs are not concurrent scheduled -->
</bean>
3. Configure the triggering mode of job scheduling
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job2" />
<property name="startDelay" value="0" /><!-- The dispatch factory is instantiated after passing through 0 The schedule starts in seconds -->
<property name="repeatInterval" value="2000" /><!-- every 2 Second scheduling 1 time -->
</bean>
4. Configure the scheduling factory
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
5. Enable 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");
}
}
A quick example of spring's built-in scheduler.
Thank you for reading, I hope to help you, thank you for your support to this site!