Spring-Teak的定时任务
Spring-Teak没有专门的包,其核心在spring-contest包中,要引用的话加入spring-contest或者Spring核心包.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
这个下面包含spring-context包.
定时任务由两种方式:一种是配置xml文件,一种是注解方式
第一种简单的xml配置模式
写一个测试类
@Component
public class SpringTask {
public void show(){
System.out.println(111);
}
}
配置xml文件
<!-- 配置自动扫描路径下的所有类,注册bean-->
<context:component-scan base-package="com"/>
<!-- 开启定时任务 -->
<task:scheduled-tasks>
<task:scheduled ref="springTask" method="show" cron = "*/1 * * * * *"/>
</task:scheduled-tasks>
注意:
这里要在spring.xml文件引入task的schema
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
结果:
每一秒输出一条记录
第二种注解方式
需要在spring.xml文件中配置
<!-- 配置自动扫描路径下的所有类,注册bean-->
<context:component-scan base-package="com"/>
<!-- 开启定时任务 -->
<task:annotation-driven/>
一样的要引入task的schema
测试:
@Component
public class SpringTask {
@Scheduled(cron = "*/1 * * * * *")
public void show(){
System.out.println(111);
}
}
结果:
注意:
这两种方法都要统一对定时任务的类进行bean注册
@Scheduled源码
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scheduling.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* An annotation that marks a method to be scheduled. Exactly one of
* the {@link #cron()}, {@link #fixedDelay()}, or {@link #fixedRate()}
* attributes must be specified.
*
* <p>The annotated method must expect no arguments. It will typically have
* a {@code void} return type; if not, the returned value will be ignored
* when called through the scheduler.
*
* <p>Processing of {@code @Scheduled} annotations is performed by
* registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be
* done manually or, more conveniently, through the {@code <task:annotation-driven/>}
* element or @{@link EnableScheduling} annotation.
*
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
* <em>composed annotations</em> with attribute overrides.
*
* @author Mark Fisher
* @author Dave Syer
* @author Chris Beams
* @since 3.0
* @see EnableScheduling
* @see ScheduledAnnotationBeanPostProcessor
* @see Schedules
*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
/**
* A cron-like expression, extending the usual UN*X definition to include
* triggers on the second as well as minute, hour, day of month, month
* and day of week. e.g. {@code "0 * * * * MON-FRI"} means once per minute on
* weekdays (at the top of the minute - the 0th second).
* @return an expression that can be parsed to a cron schedule
* @see org.springframework.scheduling.support.CronSequenceGenerator
*/
String cron() default "";
/**
* Execute the annotated method with a fixed period in milliseconds between the
* end of the last invocation and the start of the next.
* @return the delay in milliseconds
*/
long fixedDelay() default -1;
/**
* Execute the annotated method with a fixed period in milliseconds between
* invocations.
* @return the period in milliseconds
*/
long fixedRate() default -1;
/**
* Number of milliseconds to delay before the first execution of a
* {@link #fixedRate()} or {@link #fixedDelay()} task.
* @return the initial delay in milliseconds
* @since 3.2
*/
long initialDelay() default -1;
}
- cron:cron表达式
- fixedDelay:表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒
- fixedRate:从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
- initialDelay:任务第一次被调用前的延时,单位毫秒。
cron表达式解析
(cron = “* * * * * * *”)
从左到右的代表字段:秒 分 时 日期 月份 星期 年份
字段解析
字段 | 允许值 | 允许的特殊字符 |
---|---|---|
秒(Seconds) | 0~59的整数 | , - / * 四个字符 |
分(Minutes) | 0~59的整数 | , - / * 四个字符 |
小时(Hours) | 0~23的整数 | , - / * 四个字符 |
日期(DayofMonth) | 1~31的整数(但是你需要考虑你月的天数) | ,- * ? / L W C 八个字符 |
月份(Month) | 1~12的整数或者 JAN-DEC | , - * / 四个字符 |
星期(DayofWeek) | 1~7的整数或者 SUN-SAT (1=SUN) | , - * ? / L C # 八个字符 |
年(可选,留空)(Year) | 1970~2099 | , - * / 四个字符 |
特殊字符的解析
*:匹配该域的任意(每个)值。假如在Minutes域使用*, 即表示每分钟都会触发事件。简单列即为“每xx”
?:只能用于日期和星期,且只有一方能用,也是匹配任意值,但指的是符合另一方规则的任意值,相当于我随便,听你的。
备注:日期与星期有天然的冲突,例如如果我相匹配每个月的20号(DayofMonth 20),那么星期该如何处理?‘*’当然不行,这是就得使用‘?’,相当于“每个月20号,随便哪个星期”
-:表示一个范围内的任意(每个)值,如Minutes域使用5-20,表示:从5分到20分钟,每分钟触发一次。
/:表示起始时间开始触发(起始时间也会触发),每隔固定时间触发一次。例如在Minutes域使用5/20,则意味着5、25、45…触发
,:枚举出触发时间。例如:在Minutes域使用5,20,则意味着在5和20分触发
L:只能用于日期和星期,表示月/星期的最后一个匹配日(注意不是最后一天),例如:DayofWeek域使用5L,意味着在最后的一个星期四触发
W:只能用于日期,表示最近的工作日,例如:DayOfMonth域使用5W,如果5号是星期日,则匹配到下周的星期一
LW:L与W连用,但并不是概念的结合,而是此月的最后一个工作日。
#:用于DayofWeek,不仅仅表示星期几,能与数字结合,表示某个月的第几个星期几,例如4#2,即某月的第二个星期三
另外在附加一个在线cron表达式生成器