Spring Boot 定时任务

本文主要介绍了Spring Boot中使用@Scheduled建立定时任务。咱们在编写Spring Boot应用中常常会遇到这样的场景,好比:我须要定时地发送一些短信、邮件之类的操做,也可能会定时添加或者同步一些数据等。java

在Spring Boot中编写定时任务是很是简单的事情。git

①在springboot启动上面加入 @EnableScheduling注释,github

@SpringBootApplication
@EnableScheduling
public class SpringBootTaskApplication {

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

②编写类和方法。咱们在咱们真正须要执行的方法上添加了@Scheduled标注,表示这个方法是须要定时执行的。 @Scheduled注解的方法不能有返回值,而且不能有形参spring

package com.example.task.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * Created by ningcs on 17/3/27.
 */

@Component
public class ScheduledTasks {
    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Integer count0 = 1;
    private Integer count1 = 1;
    private Integer count2 = 1;

    /**
     *  只需在这里调用service便可。
     *
     * 每隔十秒进行一次
     */
    @Scheduled(cron ="*/10 * * * * *")
    public void reportCurrentTime() throws InterruptedException {
        logger.info("业务层代码");
        logger.info(String.format("---第%s次执行,当前时间为:%s", count0++, dateFormat.format(new Date())));
    }


    /**
     *  只需在这里调用service便可。
     *
     * 每隔1秒进行一次
     */
    @Scheduled(fixedRate=1000)
    public void getCurrentTime() throws InterruptedException {
        logger.info("1秒执行一次");
        logger.info(String.format("---第%s次执行,当前时间为:%s", count0++, dateFormat.format(new Date())));
    }

    /**
     *  只需在这里调用service便可。
     *
     * 每隔1秒进行一次
     */
    @Scheduled(fixedDelay=1000)
    public void CurrentTime() throws InterruptedException {
        Thread.sleep(5000);
        logger.info("隔5秒执行一次");
        logger.info(String.format("---第%s次执行,当前时间为:%s", count0++, dateFormat.format(new Date())));
    }







}

注意说明:springboot

在@Scheduled标注中,咱们使用了三种方式来实现了同一个功能:每隔5秒钟记录一次当前的时间:
 fixedRate = 5000表示每隔5000ms,Spring scheduling会调用一次该方法,不论该方法的执行时间是多少
 fixedDelay = 5000表示当方法执行完毕5000ms后,Spring scheduling会再次调用该方法
 cron = "5 * * * * * *"提供了一种通用的定时任务表达式,这里表示每隔5秒执行一次,更加详细的信息能够参考cron表达式。
 
 *CRON表达式    含义
 "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"    每一个周1、周2、周3、周4、周五的10:15触发
 */

CRON位数含义:spa

* 第一位,表示秒,取值0-59
* 第二位,表示分,取值0-59
* 第三位,表示小时,取值0-23
* 第四位,日期天/日,取值1-31
* 第五位,日期月份,取值1-12
* 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思
          另外:1表示星期天,2表示星期一。
* 第7为,年份,能够留空,取值1970-2099

cron中,还有一些特殊的符号,含义以下:code

(*)星号:能够理解为每的意思,每秒,每分,天天,每个月,每一年...
(?)问号:问号只能出如今日期和星期这两个位置,表示这个位置的值不肯定,天天3点执行,因此第六位星期的位置,咱们是不须要关注的,就是不肯定的值。同时:日期和星期是两个相互排斥的元素,经过问号来代表不指定值。好比,1月10日,好比是星期1,若是在星期的位置是另指定星期二,就先后冲突矛盾了。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
(/)斜杠:如:x/y,x是开始值,y是步长,好比在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60



 

在线Cron表达式生成器

http://cron.qqe2.com/orm

github地址:get

https://github.com/ningcs/SpringBootTask同步

相关文章
相关标签/搜索