在实际项目中需要定时循环的处理一些事情,例如定时检查数据有效性等,在此记录基于spring 简单的实现定时任务。
一、基于配置文件实现定时任务
Spring 配置文件:
<!-- 开启注解 --> <context:annotation-config /> <!-- 标注扫描的类 --> <context:component-scan base-package="cn.xuexiyuan.blog" /> <!-- 授权tonken检查刷新,每6小时执行一次 --> <task:scheduled-tasks> <task:scheduled ref="tokenRefreshTask" method="check" cron="0 0 0/6 * * ?" /> </task:scheduled-tasks>
任务执行代码:
import org.springframework.stereotype.Service;
/**
* token 检查刷新任务
* @author 左龙龙
*
* 2017年9月1日 下午5:27:04
*/
@Service(value = "tokenRefreshTask")
public class TokenRefreshTask {
public void check(){
//定时执行的方法
}
}二、基于注解方式实现定时任务
import org.springframework.stereotype.Service;
/**
* token 检查刷新任务
* @author 左龙龙
*
* 2017年9月1日 下午5:27:04
*/
@EnableScheduling
@Service(value = "tokenRefreshTask")
public class TokenRefreshTask {
@Scheduled(cron = "0 0 0/6 * * ?") //每6小时执行一次
// @Scheduled(fixedRate = 3*1000) //定时任务每3秒调用一次
public void check(){
//定时执行的方法
}
//定时任务方法执行是阻塞的,到达任务触发节点是如果之前该任务没有执行完,则会加入待执行的队列中排队等待执行
}注: Spring 在创建实例bean后发现实例bean 中方法注解标记了Scheduled就知道是定时任务的方法,因此spring 容器就将该方法根据注解内配置的信息来启用定时任务。Scheduled 注解配置任务的调度频率除了 cron、fixedRate 还有zone、fixedDelay、fixedDelayString等方式,注解源码如下:
/*
* 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 "";
/**
* A time zone for which the cron expression will be resolved. By default, this
* attribute is the empty String (i.e. the server's local time zone will be used).
* @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)},
* or an empty String to indicate the server's default time zone
* @since 4.0
* @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone)
* @see java.util.TimeZone
*/
String zone() 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 the
* end of the last invocation and the start of the next.
* @return the delay in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedDelayString() default "";
/**
* Execute the annotated method with a fixed period in milliseconds between
* invocations.
* @return the period in milliseconds
*/
long fixedRate() default -1;
/**
* Execute the annotated method with a fixed period in milliseconds between
* invocations.
* @return the period in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedRateString() default "";
/**
* 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;
/**
* Number of milliseconds to delay before the first execution of a
* {@link #fixedRate()} or {@link #fixedDelay()} task.
* @return the initial delay in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String initialDelayString() default "";
}三、Spring cron 表达式使用
Spring 配置文件中cron表达式是由 6 或 7 位由空格分开的字段组成的。从左至右,这些元素的定义如下:
域说明

实例
例1:每隔5秒执行一次:*/5 * * * * ?
例2:每隔5分执行一次:0 */5 * * * ?
在26分、29分、33分执行一次:0 26,29,33 * * * ?
例3:每天半夜12点30分执行一次:0 30 0 * * ? (注意日期域为0不是24)
每天凌晨1点执行一次:0 0 1 * * ?
每天上午10:15执行一次: 0 15 10 ? * * 或 0 15 10 * * ? 或 0 15 10 * * ? *
每天中午十二点执行一次:0 0 12 * * ?
每天14点到14:59分,每1分钟执行一次:0 * 14 * * ?
每天14点到14:05分,每1分钟执行一次:0 0-5 14 * * ?
每天14点到14:55分,每5分钟执行一次:0 0/5 14 * * ?
每天14点到14:55分,和18点到18点55分,每5分钟执行一次:0 0/5 14,18 * * ?
每天18点执行一次:0 0 18 * * ?
每天18点、22点执行一次:0 0 18,22 * * ?
每天7点到23点,每整点执行一次:0 0 7-23 * * ?
每个整点执行一次:0 0 0/1 * * ?
例4:每月1号凌晨1点执行一次:0 0 1 1 * ?
每月15号的10点15分执行一次:0 15 10 15 * ?
每月的最后一天的10:15执行一次:0 15 10 L * ?
例5:每月最后一天23点执行一次:0 0 23 L * ?
例6:每周星期天凌晨1点执行一次:0 0 1 ? * L
三月的每周三的14:10和14:44执行一次:0 10,44 14 ? 3 WED
每个周一、周二、周三、周四、周五的10:15执行一次:0 15 10 ? * MON-FRI
每月最后一个周五的10:15执行一次:0 15 10 ? * 6L
例7:2016年的每天早上10:15执行一次: 0 15 10 * * ? 2016
特殊字符说明
','字符:列出枚举值值。
例如:0 26,29,33 * * * ? 表示在26分、29分、33分执行一次。
'-'字符:指定一个值的范围。
例如:0 0-5 14 * * ? 表示每天从下午2点开始到2:05分结束,每1分钟执行一次。
'*'字符:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。
例如:0 * 14 * * ?表示每天从下午2点开始到2:59分结束,每1分钟执行一次。
'/'字符:指定一个值的增加幅度。n/m表示从n开始,每次增加m。
例如:*/5 * * * * ?表示每隔5秒执行一次。
'L'字符:用在日表示一个月中的最后一天,用在周表示该周最后一个星期,也就是周日。
例如:0 0 23 L * ?表示每月最后一天23点执行一次。
'W'字符:指定离给定日期最近的工作日(周一到周五)。
例如:在字段月域里使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份。
'LW'字符:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
'#'字符:表示该月第几个周X。例如:6#3表示该月第3个周五。
'?'字符:表示不确定的值。只能用在'月'和'周'两个域。它也匹配域的任意值,但实际不会。因为'月'和'周'会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 0 0 0 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
'C'字符:可用于'日'和'周几'字段,它是"calendar"的缩写。它表示为基于相关的日历所计算出的值(如果有的话)。如果没有关联的日历,那它等同于包含全部日历。'日'字段值为"5C"表示"日历中的第一天或者5号以后",'周几'字段值为"1C"则表示"日历中的第一天或者周日以后"。