Spring自带定时器实现定时任务

在Spring框架中实现定时任务的办法至少有2种(不包括Java原生的Timer及Executor实现方式),一种是集成第三方定时任务框架,如无处不在的Quartz;另一种便是Spring自带的定时器(仅针对3.0之后的版本)。本文将围绕Spring自带定时器,模拟实现一个最简单的定时任务,看看使用起来到底有多简单。

  1. 第二步,启动Schedule配置,XML方式的配置请自行搜索,本文仅针对注解方式的实现提供说明。

@EnableScheduling

@EnableScheduling注解,用来引入Schedule的相关配置,从其源码可见一斑。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({SchedulingConfiguration.class})
@Documented
public @interface EnableScheduling {
}

而SchedulingConfiguration的关键,则是定义ScheduledAnnotationBeanPostProcessor。顾名思义,针对基于注解的Bean组件,进行Scheduled处理。

  1. 第二步,在需要定时执行的方法上添加注解
    @Scheduled(fixedDelay = 5000L)
    @Scheduled(fixedRate = 5000L)
    @Scheduled(cron = "xxx")

阅读Scheduled源码,

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
    String cron() default "";
    String zone() default "";
    long fixedDelay() default -1L;
    String fixedDelayString() default "";
    long fixedRate() default -1L;
    String fixedRateString() default "";
    long initialDelay() default -1L;
    String initialDelayString() default "";
}

发现这个注解的Target,仅适用于方法和其他注解类型,可重复。参数包括zone - 时区,fixedRate-固定启动频率(单位毫秒),fixedDelay-固定执行周期(单位毫秒),cron-自定义Cron表达式。

fixedRate和fixedDelay的区别很简单,一个是开始时间按照一个固定的频率执行,不管之前的有没有结束,另一个从上一个任务结束到下一个任务的开始,按照固定的时间间隔。写个简单的sample看下效果:

@Component
public class SpringTaskDemo {

    private int round = 0;

    @Scheduled(fixedRate = 5000L)
    public void counting(){
        round++;
        System.out.println(">>>>>>>>>" + "Counting Round " + round);
        System.out.println("Start at : " + new DateFormatter("yyyy-MM-dd HH:mm:sss").print(new Date(), Locale.CHINESE));
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finish at: "+ new DateFormatter("yyyy-MM-dd HH:mm:sss").print(new Date(), Locale.CHINESE));
    }
}

fixedRate 效果如下:

>>>>>>>>>Counting Round 1
Start at : 2018-09-03 00:28:023
Finish at: 2018-09-03 00:28:026
>>>>>>>>>Counting Round 2
Start at : 2018-09-03 00:28:028
Finish at: 2018-09-03 00:28:031
>>>>>>>>>Counting Round 3
Start at : 2018-09-03 00:28:033
Finish at: 2018-09-03 00:28:036
>>>>>>>>>Counting Round 4
Start at : 2018-09-03 00:28:038
Finish at: 2018-09-03 00:28:041
>>>>>>>>>Counting Round 5
Start at : 2018-09-03 00:28:043
Finish at: 2018-09-03 00:28:046

fixedDelay效果如下:

>>>>>>>>>Counting Round 1
Start at : 2018-09-03 00:30:031
Finish at: 2018-09-03 00:30:034
>>>>>>>>>Counting Round 2
Start at : 2018-09-03 00:30:039
Finish at: 2018-09-03 00:30:042
>>>>>>>>>Counting Round 3
Start at : 2018-09-03 00:30:047
Finish at: 2018-09-03 00:30:050
>>>>>>>>>Counting Round 4
Start at : 2018-09-03 00:30:055
Finish at: 2018-09-03 00:30:058
>>>>>>>>>Counting Round 5
Start at : 2018-09-03 00:31:003
Finish at: 2018-09-03 00:31:006

这么看起来,其自带的定时任务,还是很准时的。cron表达式支持定义更为复杂的任务周期,关于cron的例子不再枚举,请自行搜索并测试。

猜你喜欢

转载自blog.51cto.com/10705830/2169251