Spring Task

来源:互联网 发布:淘宝买家秀木耳图链接 编辑:程序博客网 时间:2024/05/29 14:56

Spring Task 是 Spring3.0 以后自主开发的定时任务工具,相当于一个轻量级的Quartz,使用起来相当简单,也不需要依赖额外的包,支持注解和配置文件两种形式

编写一个普通的JavaBean

import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.stereotype.Component;@Component("scheduledTasks")public class ScheduledTasks {    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");    public void reportCurrentTime() {        System.out.println("The time is now " + dateFormat.format(new Date()));    }}

Spring配置文件添加task命名空间和设置任务定时

<?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:context="http://www.springframework.org/schema/context"    xmlns:task="http://www.springframework.org/schema/task"    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan base-package="com.dragon" />    <task:scheduled-tasks>        <task:scheduled ref="scheduledTasks" method="reportCurrentTime"            cron="0 * * * * ?" />    </task:scheduled-tasks></beans>

如上配置即可

若使用注解形式

import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Component("scheduledTasks")public class ScheduledTasks {    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");    @Scheduled(fixedRate = 5000)    public void reportCurrentTime() {        System.out.println("The time is now " + dateFormat.format(new Date()));    }}

Spring配置文件

<?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:context="http://www.springframework.org/schema/context"    xmlns:task="http://www.springframework.org/schema/task"    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan base-package="com.dragon" />   <task:annotation-driven/></beans>

Scheduled注解参数:
cron:cron时间表达式,按表达式定义的规则调度执行任务
zone:定义时区,默认为服务器本地时区
fixedDelay:表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒,long类型参数
fixedDelayStirng:表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒,String类型参数
fixedRate:从上一个任务开始到下一个任务开始的间隔,单位是毫秒,long类型参数
fixedRateString:从上一个任务开始到下一个任务开始的间隔,单位是毫秒,String类型参数
initialDelay:任务第一次执行的延迟时间,单位是毫秒,long类型参数
initialDelayString:任务第一次执行的延迟时间,单位是毫秒,String类型参数

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 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 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 between invocations.     * @return the period in milliseconds     */    long fixedRate() default -1;    /**     * Execute the annotated method with a fixed period 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 "";}

cron表达式规则:
这里写图片描述

反斜线(/)字符表示增量值。例如,在秒字段中“5/15”代表从第 5 秒开始,每 15 秒一次。
问号(?)字符和字母 L 字符只有在月内日期和周内日期字段中可用。问号表示这个字段不包含具体值。所以,如果指定月内日期,可以在周内日期字段中插入“?”,表示周内日期值无关紧要。字母 L 字符是 last 的缩写。放在月内日期字段中,表示安排在当月最后一天执行。在周内日期字段中,如果“L”单独存在,就等于“7”,否则代表当月内周内日期的最后一个实例。所以“0L”表示安排在当月的最后一个星期日执行。
在月内日期字段中的字母(W)字符把执行安排在最靠近指定值的工作日。把“1W”放在月内日期字段中,表示把执行安排在当月的第一个工作日内。
井号(#)字符为给定月份指定具体的工作日实例。把“MON#2”放在周内日期字段中,表示把任务安排在当月的第二个星期一。
星号(*)字符是通配字符,表示该字段可以接受任何可能的值。

常用示例:
这里写图片描述

0 0