Springboot之使用Scheduled做定时任务

定时任务有好多开源框架比如Quartz,@Scheduled是Spring的一个定时任务注解,通过注解配置就能够轻量级的定时任务,简单方便。

一、@Scheduled注解介绍

这里先贴上@Scheduled注解。然后下面的这几个属性的介绍。

 * Copyright 2002-2018 the original author or authors.

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
     * or a {@link java.time.Duration#parse java.time.Duration} compliant value
     * @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
     * or a {@link java.time.Duration#parse java.time.Duration} compliant value
     * @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
     * or a {@link java.time.Duration#parse java.time.Duration} compliant value
     * @since 3.2.2
     */
    String initialDelayString() default "";

}

cron属性
    这是一个时间表达式,可以通过简单的配置就能完成各种时间的配置,我们通过CRON表达式几乎可以完成任意的时间搭配,它包含了六或七个域:
    Seconds : 可出现", - * /"四个字符,有效范围为0-59的整数
    Minutes : 可出现", - * /"四个字符,有效范围为0-59的整数
    Hours : 可出现", - * /"四个字符,有效范围为0-23的整数
    DayofMonth : 可出现", - * / ? L W C"八个字符,有效范围为0-31的整数
    Month : 可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
    DayofWeek : 可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
    Year : 可出现", - * /"四个字符,有效范围为1970-2099年
    "0 0 12 * * ?"    每天中午十二点触发
    "0 15 10 ? * *"    每天早上10:15触发
    "0 15 10 * * ?"    每天早上10:15触发
    "0 15 10 * * ? *"    每天早上10:15触发
    "0 15 10 * * ? 2005"    2005年的每天早上10:15触发
    "0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发
    "0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发
    "0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
    "0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发
    "0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发
    "0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发

fixedRate属性
    上一个调用开始后再次调用的延时(不用等待上一次调用完成),这样就会存在重复执行的问题,所以不是建议使用,但数据量如果不大时在配置的间隔时间内可以执行完也是可以使用的。

fixedDelay属性
    该属性的功效与上面的fixedRate则是相反的,配置了该属性后会等到方法执行完成后延迟配置的时间再次执行该方法。

initialDelay属性
    该属性跟上面的fixedDelay、fixedRate有着密切的关系,为什么这么说呢?该属性的作用是第一次执行延迟时间,只是做延迟的设定,并不会控制其他逻辑,所以要配合fixedDelay或者fixedRate来使用。

二、@Scheduled注解实例

package com.example.demo;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulerTask {
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 10000)
    public void timerRate() {
        System.out.println(dateFormat.format(new Date()));
    }

    //第一次延迟1秒执行,当执行完后2秒再执行
    @Scheduled(initialDelay = 1000, fixedDelay = 2000)
    public void timerInit() {
        System.out.println("init : "+dateFormat.format(new Date()));
    }

    //每天21点41分50秒执行
    @Scheduled(cron = "50 41 21 * * ?")
    public void timerCron() {
        System.out.println("current time : "+ dateFormat.format(new Date()));
    }
}

代码都使用了上了的@Scheduled属性。下图是执行打印的日志信息,timerInit方法是每隔2秒执行,timerCron设置在21点41分50秒执行,timerRate是每隔10秒执行。

三、添加@EnableScheduling注解

上面的定时任务并不能执行,还需要一步,需要设置开启定时任务,这个也很简单,只需在main方法上加上@EnableScheduling注解即可。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

原文地址:https://www.cnblogs.com/5ishare/p/9368546.html

时间: 2024-08-30 05:22:22

Springboot之使用Scheduled做定时任务的相关文章

SpringBoot中使用@Scheduled创建定时任务

SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回调,这种回调一般都是定时任务来完成的.还有就是报表的生成,我们一般会在客户访问量过小的时候来完成这个操作,那往往都是在凌晨.这时我们也可以采用定时任务来完成逻辑.SpringBoot为我们内置了定时任务,我们只需要一个注解@Scheduled就

SpringBoot中使用Scheduling执行定时任务

SpringBoot自带的 Schedule,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多以下任务都是在单线程下执行的第一步 创建SpringBoot项目第二步 添加@EnableScheduling开启定时任务第三步 设置定时需要执行的任务有两种方法设置执行时机第一种我们就且叫他为普通方法1.fixedRate:会为所有任务的开始执行时间编排一个表,假如fixedRate=5000,且第一次开始时间是10:00:00任务 开始执行时间任务1 10:00:00任务2

JFinal - scheduler 插件做定时任务

基于 jfinal 框架做定时任务可以使用插件 jfinal-scheduler 插件来解决. 下面是我在项目中使用 jfinal-scheduler 的例子. 配置插件,在自定义的 JFinalConfig 类中引入此插件: /** * 配置插件 */ public void configPlugin(Plugins me) { // ... // 定时器插件 SchedulerPlugin sp = new SchedulerPlugin(); Runnable task = new Tas

spring注解scheduled实现定时任务

只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springframework.org/schema/task"以及在xsi:schemaLocation中加入 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd

ETL工具kettle怎么做定时任务

kettle做定时任务平时任务中经常需要用两种方法:一.kettle自带的功能.具体操作如下:首先形成一个job,然后点击开始组件:结果如图:当需要定时时,那么就是需要重复:此时勾选重复选项,然后点击类型下拉选选择你需要定时的类型,如时间间隔,天,周,月:如果选择时间间隔,意味着你每隔多长时间执行一次,单位是分秒,自行选择.如图当选择了天,那么就是意味着在每天的几点执行,如图当选择了周,就意味这是在每周的几点执行当选择了月,意味着是在每月的多少号的几点执行当然这时一种单选,选择之后一定记得点击确

springboot+springmvc拦截器做登录拦截

springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration 实现 WebMvcConfigurer 接口,注册拦截器 ResourceBundle 加载 properties文件数据,配置不进行拦截的路径 LoginInterceptor package com.ytkj.smart_sand.system.interceptor; import com.

spring-boot实战【09】【转】:Spring Boot中使用@Scheduled创建定时任务

我们在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. 创建定时任务 在Spring Boot中编写定时任务是非常简单的事,下面通过实例介绍如何在Spring Boot中创建定时任务,实现每过5秒输出一下当前时间. 在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 1 2 3 4 5 6 7 8 9 10 @SpringBootApplication @E

Springboot中做定时任务 和 Springboot API 分页

定时任务 Springboot API 分页

SpringBoot系列:Spring Boot定时任务Spring Schedule

Spring Schedule是Spring提供的定时任务框架,相较于Quartz,Schedule更加简单易用,在中小型应用中,对于大部分需求,Schedule都可以胜任. 一.Spring Schedule使用演示 在 SpringBoot使用Spring Schedule非常简单,因为SpringBoot自身的starter中已经集成了Schedule,而不需要我们做更多的处理. 使用@EnableScheduling注解开启定时功能,该注解可以使用在启动类上,也可以注解于定时任务的类上.