Internal Spring Spring is a task that comes with a set time automatic task scheduling, configuration provides two ways, one is the annotation of the way, and the other is the XML configuration. Notes manner is relatively simple, XML configuration relatively somewhat cumbersome, but different application scenarios, and both have their advantages, so the specific use or divided according to need. Because of this demand task scheduling, changes are usually more, if the same general approach change becomes trouble, have to be recompiled. So more often choose to use XML configuration mode.
Here are just some configuration in two ways:
The first way: XML configuration
The first step: writing job classes
Import org.springframework.stereotype.Service; @Service public class TaskJob { public void the jobs that job1 () { System.out.println ( "tasks in ..."); } }
Step Two: Adding spring-task configuration file associated header information is as follows:
<?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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:scheduled-tasks> <task:scheduled ref="taskJob" method="job1" cron="0 0 5 * * ?"/> </task:scheduled-tasks> </beans>
Description: ref parameter specifies the task class, method specifies the method needs to run
The third step: introduction of spring-task profile in the spring profile
. . . . . <! - the introduction of Spring's mission profile. -> < Import Resource = "where xxx.xml" /> . . . . .
Such configuration is complete, you can test verified.
The second: use annotations in the form of
@Scheduled annotated source file definition:
@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(); }
The annotation can be seen that there are three methods or parameters called, respectively, the meaning is:
cron: cron expression specified
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 means that complete from a task starts to distance the next task begins,. in milliseconds.
fixedRate: official documentation explains: An interval-based trigger where the interval is measured from the start time of the previous task The time unit value is measured in milliseconds starts from one task to the interval next task starts, the unit is. millisecond.
The configuration procedure:
The first step: writing job classes
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(“任务进行中。。。”); } }
Step two: Open the task in spring configuration file:
<task:annotation-driven/>
This realization of the way of configuration annotations.
cron expression and cronExpression
cronExpression configuration description field allows values allow special characters in seconds 0-59, - * / min 0-59, - * / h 0-23, - * / Date 1-31, -? * / LWC March 1-12 or JAN-DEC, - * / week 1-7 or SAT-SUN, -? * / LC # years (optional) left blank, 1970-2099, - * / - interval * wildcard ? you do not want to set that field only examples below a few formulas CRON expression meaning "0012 * *?" every day noon trigger "01510? * *" 10:15 every morning trigger "01510 * *?" 10:15 every morning trigger "01510 * * *?" 10:15 every morning trigger "01510 * *? 2005" 2005 10:15 every morning trigger "0 * 14 * *?" Every day from the beginning of 14:00 to 2:59 trigger once every minute "00 / * 514 *? " every day from 14:00 to 2:55 o'clock the start trigger once every 5 minutes " 0 0/5 * 14, 18 *? " the daily 14:00 to 2:55 and 6:00 to 6 55 points in two time periods triggered once every 5 minutes "00-514 * *?" every day from 14:00 to 14:05 the trigger once every minute "0 10,44 14? 3 WED" every Wednesday in March the 14:10 and 14:44 triggering "0 15 10? * MON- FRI" every Monday, Tuesday, Wednesday, Thursday, Friday triggered 10:15