I. Preface
The above two introduces the use of timer and quartz in spring, this article will introduce the Spring3.0 self-developed Time Task tool, spring task, can compare it to a lightweight quartz, and it is very simple to use. There is no need for additional packages outside of spring-related packages, and there are two forms of annotations and configuration files, both of which are described below.
Second, the first type: Configuration file mode
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: Spring configuration
<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 "> <task:scheduled-tasks> <task:scheduled ref=" Taskjob "method=" Job1 "cron = "0 * * * *?" /> </task:scheduled-tasks> <context:component-scan base-package= "Com.gy.mytask"/></ Beans>
Description: The ref parameter specifies the task class, method specified, which is required to run the methods, cron, and cronexpression expressions. <context:component-scan base-package= "Com.gy.mytask"/> This configuration is not much said, spring scanning annotations used.
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: Specifies the cron expression.
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.
First step: Writing Pojo
Import org.springframework.stereotype.Component; @Component ("Taskjob") public class Taskjob { @Scheduled (cron = "0 0 3 * *?") public void Job1 () { System.out.println ("task in progress ... "); } }
Step Two: Add the task-related configuration:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "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" xmln s:task= "Http://www.springframework.org/schema/task" xsi:schemalocation= "Http://www.springframework.org/sche Ma/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://w Ww.springframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/task HTTP://WWW.SPRINGF Ramework.org/schema/task/spring-task-3.0.xsd "default-lazy-init=" false "> <context:annotation-config/> <!-spring scan annotation configuration, <context:component-scan base-package= "Com.gy.mytask"/> <!-on this configuration, spring recognizes the @scheduled annotation--& Lt;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.
After configuration, spring task has many parameters, refer to XSD document http://www.springframework.org/schema/task/spring-task-3.0.xsd
The Spring-task of spring task scheduling