Several implementations of spring timed tasks
Recent project development needs to perform some scheduled tasks, such as the need to analyze the log information of the day before the morning, to take this opportunity to collate some of the timing of the implementation of the task, because the project uses the spring framework, so I will combine
Spring Framework to introduce.
A Classification
- From the implementation of the technology to classify, there are currently three kinds of technology (or three products):
- Java comes with the Java.util.Timer class, which allows you to dispatch a Java.util.TimerTask task. Using this method allows your program to execute at a certain frequency, but not at a specified time. Generally used less, this article will not do a detailed introduction.
- Using quartz, this is a powerful scheduler that allows your program to execute at a specified time, and can be executed at a certain frequency, which is slightly more complex to configure, and will be described in more detail later.
- SPRING3.0 's own task, which can be seen as a lightweight quartz, is much simpler to use than quartz and will be introduced later.
- From the inheritance of the job class, you can be divided into two categories:
- 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: A second approach is recommended, because the classes are generic and do not need to be treated differently beforehand.
- 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.
Two Usage Notes
Details how each task scheduling tool is used, including quartz and spring task two.
Quartz first, the job class inherits from a specific base class: Org.springframework.scheduling.quartz.QuartzJobBean.
First step: 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;
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 ("Timed Task Execution ...");
}
}
Step Two: Configure the job class in the spring configuration file Jobdetailbean
<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:
<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" >
<property name= "Jobdetail" ref= "Job1"/>
<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>
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:
<bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" ref= "Job1"/>
<!-run one time every 12:00
<property name= "cronexpression" value= "0 0 * *?"/>
</bean>
See appendix for syntax for cronexpression expressions.
Fourth Step: Configure the Dispatch factory
<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!
See my introduction to Spring task in the following section.
Spring-task
The previous section describes the use of quartz in spring, this article describes Spring3.0 's self-developed scheduled Task tool, spring task, which can be compared to a lightweight quartz, and is simple to use, except that spring-related packages do not require additional packages. and two types of annotations and configuration files are supported
form, the two approaches are described below.
First: How to configure Files
First step: Writing Job classes
That is, the ordinary Pojo, as follows:
Import Org.springframework.stereotype.Service;
@Service
public class Taskjob {
public void Job1 () {
SYSTEM.OUT.PRINTLN ("Task in progress ... ”);
}
}
Step Two: Add namespaces and descriptions to the spring configuration file 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 three: Set up specific tasks in the spring configuration file
<task:scheduled-tasks>
<task:scheduled ref= "Taskjob" method= "job1" cron= "0 * * * * *?" />
</task:scheduled-tasks>
<context:component-scan base-package= "Com.gy.mytask"/>
Description: The ref parameter specifies the task class, method specified is required to run methods, cron and cronexpression expression, the specific wording is not introduced here, the details of the above article appendix.
<context:component-scan base-package= "Com.gy.mytask"/> This configuration is not much said, spring scanning annotations used.
The configuration is complete here, it is not very simple.
Second: Use annotations as a form of
Maybe we don't want to write a task class to be configured in an XML file, we can use the annotation @scheduled, and we'll look at the definition of the annotation in the source file:
@Target ({Java.lang.annotation.ElementType.METHOD, Java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention (Retentionpolicy.runtime)
@Documented
Public @interface Scheduled
{
Public abstract String cron ();
Public abstract long Fixeddelay ();
Public abstract long fixedrate ();
}
It can be seen that the note has three methods or parameters, respectively, that means:
Cron: Specifying cron expressions
Fixeddelay: Official documentation explains: An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds. That is, the interval from the start of the previous task to the start of the next task, in milliseconds.
Fixedrate: Official documentation explains: An interval-based trigger where the interval are measured from the start time of the previous task. The time unit value is measured in milliseconds. The interval from the start of the previous task to the start of the next task, in milliseconds.
Let me configure it below.
First step: Writing Pojo
Import org.springframework.scheduling.annotation.Scheduled;
Import org.springframework.stereotype.Component;
@Component ("Taskjob")
public class Taskjob {
@Scheduled (cron = "0 0 3 * *?")
public void Job1 () {
SYSTEM.OUT.PRINTLN ("Task in progress ... ”);
}
}
<?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= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "
Default-lazy-init= "false" >
Step Two: Add the task-related configuration:
<?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= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "
Default-lazy-init= "false" >
<context:annotation-config/>
Configuration of <!-spring scan annotations--
<context:component-scan base-package= "Com.gy.mytask"/>
<!-Open this configuration, spring will recognize @scheduled annotations--
<task:annotation-driven scheduler= "Qbscheduler" mode= "proxy"/>
<task:scheduler id= "Qbscheduler" pool-size= "ten"/>
Description: Theoretically only need to add <task:annotation-driven/> This sentence configuration can be, these parameters are not necessary.
OK configuration is complete, of course, Spring task has a lot of parameters, I do not explain, specifically refer to the XSD document HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TASK/SPRING-TASK-3.0.XSD.
Original address: http://gong1208.iteye.com/blog/1773177
Several implementations of spring timed tasks