Several realization methods of java Quartz timer task and Spring task timing

  • 2020-06-15 08:32:18
  • OfStack

1. classification

In terms of implementation technologies, there are three main technologies (or three products) :

Java comes with the java.util.Timer class, which allows you to schedule 1 java.util.TimerTask task. This allows your program to execute at a certain frequency, but not at a specified time. 1 generally used less, this article will not go into details.

2. Use Quartz, a powerful scheduler that allows your program to execute at a specified time or at a specified frequency. This is a little more complicated to configure, which will be explained later.

3. Spring 3.0 comes with task, which can be regarded as a lightweight Quartz, and it is much simpler to use than Quartz, which will be introduced later.

From the inheritance of job class, it can be divided into two types:

1. The working class to inherit from a specific job class base class, such as Quartz need to inherit from org. springframework. scheduling. quartz. QuartzJobBean; java.util.Timer needs to be inherited from java.util.TimerTask.

2. The job class is the common java class, which does not need to be inherited from any base class.

Note: It is personally recommended to use the second method, because all classes in this way are ordinary classes, do not need to be treated differently in advance.

From the trigger time of task scheduling, here is mainly for the job triggers, there are mainly the following two types:

1. Every time the triggering 1 time, in the Quartz corresponding trigger for: org. springframework. scheduling. quartz. SimpleTriggerBean

2. Each to a specified time is triggered, in Quartz corresponding scheduler is: org. springframework. scheduling. quartz. CronTriggerBean

Note: Not every task can use both triggers. For example, the ES66en.util.TimerTask task can only use the first one. Both Quartz and spring task can support both triggers.

2. instructions

The usage of each task scheduling tool is described in detail, including Quartz and spring task.

Quartz

1 species, homework to class inherits from a specific base class: org. springframework. scheduling. quartz. QuartzJobBean.

Step 1: 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;
// 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(" Timed task execution... ");
}
}

Step 2: Configure the job class JobDetailBean in the spring configuration file


<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 attributes, jobClass attributes that we defined in java code task class, jobDataAsMap property that is the task need to inject in the class of attribute values.

Step 3: Configure the firing mode (trigger) for job scheduling

There are two types of JOB triggers for Quartz, respectively

org.springframework.scheduling.quartz.SimpleTriggerBean

org.springframework.scheduling.quartz.CronTriggerBean

The first, SimpleTriggerBean, only supports calling tasks at a fixed frequency of 1, such as running every 30 minutes.

The configuration method is as follows:


<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job1" />
<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>

The second type, CronTriggerBean, supports running once to a specified time, such as running once at 12:00 per day, etc.

The configuration method is as follows:


<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job1" />
<! Every day - 12:00 run 1 time  -->
<property name="cronExpression" value="0 0 12 * * ?" />
</bean>

Step 4: Configure the scheduling factory


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>

Note: This parameter specifies the name of the previously configured trigger.

Step 5: Just start your application and deploy the project to tomcat or another container.

Second, a job class does not inherit from a particular base class.

Spring supports this approach thanks to two classes:


org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean

org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

These two classes correspond to two ways of implementing task scheduling supported by spring, namely, timer task and Quartz, which come with java as mentioned above. I'll just write the use of MethodInvokingJobDetailFactoryBean here, and the advantage of using this class is that our task class no longer needs to inherit from any class, but the normal pojo.

Step 1: Write the task class


public class Job2 {
public void doJob2() {
System.out.println(" No inheritance QuartzJobBean way - Scheduling in progress ...");
}
}

As you can see, this is a normal class with a method.

Step 2: Configure the job classes


<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" /><!--  Jobs are not concurrent scheduled  -->
</bean>

Note: This step is the key step. Declare an MethodInvokingJobDetailFactoryBean with two key properties: targetObject specifies the task class and targetMethod specifies the method to run. The following steps are the same as in method 1, and are posted for completeness.

Step 3: Configure the firing mode (trigger) for job scheduling

There are two types of Quartz job triggers, respectively


org.springframework.scheduling.quartz.SimpleTriggerBean

org.springframework.scheduling.quartz.CronTriggerBean

The first, SimpleTriggerBean, only supports calling tasks at a fixed frequency of 1, such as running every 30 minutes.

The configuration method is as follows:


<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>

The second type, CronTriggerBean, supports running once to a specified time, such as running once at 12:00 per day, etc.

The configuration method is as follows:


<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>
0

According to the actual situation, either of the above two scheduling modes can be selected.

Step 4: Configure the scheduling factory


<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>
1

Note: This parameter specifies the name of the previously configured trigger.

Step 5: Just start your application and deploy the project to tomcat or another container.

This is the end of the basic configuration of Quartz in spring, of course, before use, to import the corresponding package of spring and Quartz, which goes without saying.

As you can see, Quartz's configuration looks complicated, and there is no way, because Quartz is actually a heavyweight tool. If we just want to simply perform a few simple timing tasks, is there a simpler tool? Yes!

Spring-Task

This article introduces spring task, a self-developed timing task tool since Spring3.0, which can be compared to a lightweight Quartz. It is also very simple to use, no additional packages are required except spring-related packages, and it supports both annotations and configuration files

Form. These two approaches are described below.

Type 1: Configuration file approach

Step 1: Write the job class

Common pojo, as follows:


<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>
2

Step 2: Add the namespace and description to the spring profile header


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:task="http://www.springframework.org/schema/task" 
  . 
 xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

Step 3: Set specific tasks in the spring configuration file


<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>
4

Description: the ref parameter specifies the task class, method specifies the method to be run, cron and cronExpression expressions, the specific method is not described here, see the appendix of the previous article for details.

< context:component-scan base-package="com.gy.mytask" / > Needless to say, this configuration is for spring scan annotations.

That's it. That's it. That's it.

Type 2: Use annotated form

Maybe we don't want to configure the xml file for every task class we write, we can use the annotation @Scheduled, let's look at the definition of the annotation in the source file:


<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>
5

As you can see, the annotation has three methods or parameters, each of which means:

cron: Specifies the cron expression

fixedDelay: Official document Explanation: An interval where is is time time time time time time time milliseconds. Represents the interval in milliseconds from the completion of the previous task to the start of the next task.

fixedRate: Official document Explanation: An trigger where from of of the of the the the the the the the the the the the the the the the the the the the the the the the the the the the milliseconds. The interval from the beginning of the previous task to the beginning of the next task in milliseconds.

So let me configure 1.

Step 1: Write pojo


<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>
6

Step 2: Add task related configuration:


<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>
7

Note: In theory, just add < task:annotation-driven / > These parameters are not required.


Related articles: