Home >Java >javaTutorial >Usage instructions for Spring scheduled tasks
Java’s own java. util.Timer class, this class allows you to schedule a java.util.TimerTask task. Using this method allows your program to be executed at a certain frequency, but not at a specified time.
Use Quartz, which is a relatively powerful scheduler that allows your program to be executed at a specified time or at a certain frequency. The configuration is a bit complicated. .
The task that comes with Spring 3.0 and later can be regarded as a lightweight Quartz, and it is much simpler to use than Quartz.
The job class needs to inherit from a specific job class base class. For example, Quartz needs to inherit from org.springframework.scheduling.quartz.QuartzJobBean; java.util.Timer needs to inherit from java.util.TimerTask.
The job class is an ordinary java class and does not need to inherit from any base class.
Note: I personally recommend using the second method, because all classes are common classes and do not need to be treated differently in advance.
Triggers once every specified time. The corresponding trigger in Quartz is: org.springframework.scheduling.quartz.SimpleTriggerBean
Every It will be triggered once at the specified time. The corresponding scheduler in Quartz is: org.springframework.scheduling.quartz.CronTriggerBean
Note: Not every task can use these two triggers. Devices such as java.util.TimerTask tasks can only use the first one. Both Quartz and spring task can support these two trigger conditions.
Introduces in detail how to use each task scheduling tool, including Quartz and spring task.
Step 1: Define the job class
import org.quartz.JobExecutionContext;
##import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
class Job1 extends QuartzJobBean {
int i = 0;
##public
##this.timeout = timeout;
}
* Specific tasks to be scheduled
*/
##@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
"The scheduled task is being executed...");
}
bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean"> #property name= "jobClass" value="com.gy.Job1" />
>
## entry"timeout" value="0" /> ##
mapproperty
##bean
>Description: org.springframework.scheduling.quartz.JobDetailBean has two attributes, the jobClass attribute is what we use in java For the task class defined in the code, the jobDataAsMap attribute is the attribute value that needs to be injected into the task class.
There are two types of job triggers in Quartz, namely
org .springframework.scheduling.quartz.SimpleTriggerBeanorg.springframework.scheduling.quartz.CronTriggerBean
The first SimpleTriggerBean only supports calling tasks at a certain frequency, such as running once every 30 minutes. .
The configuration method is as follows:
Xml code
bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
#property name= "jobDetail" ref="job1" />
>
## See the appendix for the syntax of cronExpression expression.Xml code
"org.springframework.scheduling.quartz.SchedulerFactoryBean"> #property
> ##list
># #ref bean=
"cronTrigger"#list>
##property >
#bean>
## Description: This parameter specifies the name of the previously configured trigger. Step 5: Just start your application, that is, deploy the project to tomcat or other containers.
org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean
org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBeanThese two classes respectively correspond to the two methods of task scheduling supported by spring, namely the timer task method and the Quartz method that come with java as mentioned above. Here I only write about the usage of MethodInvokingJobDetailFactoryBean. The advantage of using this class is that our task class no longer needs to inherit from any class, but is an ordinary pojo.public
class Job2 {System.out.println(
"Does not inherit QuartzJobBean mode - scheduling is in progress...");}
It can be seen that this is an ordinary class and has one method.
Step 2: Configure job class
bean id="job2"
##class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
property name="targetObject">
property>
name="targetMethod" value="doJob2" />
propertyname="concurrent" value="false" />
#bean## Description: This step is a critical step, declare A MethodInvokingJobDetailFactoryBean has two key attributes: targetObject specifies the task class, and targetMethod specifies the running method. The following steps are the same as method 1. For the sake of completeness, they are also posted.
org .springframework.scheduling.quartz.SimpleTriggerBean
org.springframework.scheduling.quartz.CronTriggerBeanThe first SimpleTriggerBean only supports calling tasks at a certain frequency, such as running once every 30 minutes. . The configuration method is as follows: Xml codebean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
#property name= "jobDetail" ref="job2" />
>
##You can choose any one of the above two scheduling methods according to the actual situation.Xml code
"org.springframework.scheduling.quartz.SchedulerFactoryBean"> #property
> ##list
># #ref bean=
"cronTrigger"#list>
##property >
#bean>
##Explanation: This parameter specifies the name of the previously configured trigger. Step 5: Just start your application, that is, deploy the project to tomcat or other containers.
Spring-TaskThe previous section introduced the use of Quartz in Spring. This article introduces the self-developed scheduled task tool after Spring 3.0, spring task, which can be It is like a lightweight Quartz, and it is very simple to use. It does not require additional packages except spring-related packages, and it supports two forms of annotations and configuration files. These two will be introduced below. Way. The first one: configuration file method
The first step: write the job class
That is the ordinary pojo, as follows: Java codeimport org.springframework.stereotype.Service;
@Service
public class TaskJob {
#public void job1() {
Step 2: Add the namespace and Description
. . . . . .
##
xsi:schemaLocation=
Xml code
Maybe we don’t want to configure it in the xml file every time we write a task class. We can use the annotation @Scheduled. Let’s take a look at the source file. Definition of annotation:
Java code
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
##@Documented
public @interface Scheduled
public abstract String cron() ;
abstract long fixedDelay();
long fixedRate();
}Step one: Write pojo
Java code
##import org.springframework.stereotype.Component;
##@Component(“taskJob”)
##public class TaskJob {
##@Scheduled(cron = "0 0 3 * * ?")
# PUBLIC VOID JOB1 () {
## System.out.println ("Mission is in progress. . ”); # }
Xml code
xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
"
default-lazy-init="false">
context:annotation-config />
>
context:component-scan base-package="com.gy.mytask" />
>
task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
task:scheduler id="qbScheduler" pool-size="10"/>
Note: In theory, you only need to add the configuration sentence
Ok configuration is complete. Of course, the spring task still has many parameters. I will not explain them one by one. Please refer to the xsd document for details.
Appendix: Configuration instructions for
cronExpression, please refer to Baidu google
field for specific usage and parameters. Allowed values Allowed special characters
seconds 0-59 , - * /
minutes 0-59 , - * /
Hour 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
? You don’t want to set that field
The following are just a few examples
CRON expression Formula Meaning
"0 0 12 * * ?" Triggered at 12 noon every day
"0 15 10 ? * *" Every morning Triggered at 10:15
"0 15 10 * * ?" Triggered at 10:15 every morning
"0 15 10 * * ? * " Triggered every morning at 10:15
"0 15 10 * * ? 2005" Triggered every morning at 10:15 in 2005
" 0 * 14 * * ?" Triggered every minute from 2:00 pm to 2:59 every day
"0 0/5 14 * * ?" Every day from 2 pm Triggered every 5 minutes until the end of 2:55
"0 0/5 14,18 * * ?" Every day from 2 pm to 2:55 and 6 pm to 6 pm Triggered every 5 minutes in two time periods of 55 minutes
"0 0-5 14 * * ?" Triggered once every minute from 14:00 to 14:05 every day
"0 10,44 14 ? 3 WED" Triggered every Wednesday at 14:10 and 14:44 in March
"0 15 10 ? * MON-FRI" is triggered every Monday, Tuesday, Wednesday, Thursday and Friday at 10:15
The above is the detailed content of Usage instructions for Spring scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!