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

定时任务通常会在不少项目中都会用到,咱们每每会间隔性的的去完成某些特定任务来减小服务器和数据库的压力。比较常见的就是金融服务系统推送回调,通常支付系统订单在没有收到成功的回调返回内容时会持续性的回调,这种回调通常都是定时任务来完成的。还有就是报表的生成,咱们通常会在客户访问量太小的时候来完成这个操做,那每每都是在凌晨。这时咱们也能够采用定时任务来完成逻辑。SpringBoot为咱们内置了定时任务,咱们只须要一个注解@Scheduled就能够开启定时任务了。web

下面咱们来经过SpringBoot项目熟悉一下具体实现spring

一,在pom.xml文件中加入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
复制代码

二,建立启动类上添加注解@SpringBootApplication@EnableScheduling

@SpringBootApplication
@EntityScan(basePackages= {"cn.sh.sttri.app.messagelistener.*.entity"})
@EnableCaching
@EnableScheduling
public class MessagelistenerApplication {

	public static void main(String[] args) {

		SpringApplication.run(MessagelistenerApplication.class, args);

	}
}
复制代码

三,建立类ScheduledTask用来演示定时任务

使用@scheduled注解来演示定时执行任务的方式 咱们经过@scheduled注解用来配置到方法上来完成对应的定时任务的配置,如执行时间,间隔时间,延迟时间等等,下面咱们就来详细的看下对应的属性配置。 @Component这个注解能够在程序启动时自动将该类加载进来数据库

fixedDelay属性

该属性是程序启动后每3000ms执行一次bash

/**
 * Created by zhangshuai on 2018/11/14 .
 */

@Component
public class ScheduledTask {

    @Autowired
    public QuartzJobService quartzJobService;

    private static int count1=1;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Scheduled(fixedDelay = 3000)
    public void fixedDelay() {
        System.out.println(String.format("1第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date(System.currentTimeMillis()))));
    }

}
复制代码

执行结果以下 服务器

/**
   * 天天中午十二点触发
   */
    @Scheduled(cron="0 0 12 * * ?")
    public void cron() {
        System.out.println(String.format("1第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date(System.currentTimeMillis()))));
    }
复制代码

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年app

下面简单举几个例子:spring-boot

"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触发spa

fixedRate属性

该属性的含义是上一个调用开始后再次调用的延时(不用等待上一次调用完成),这样就会存在重复执行的问题,因此不是建议使用,但数据量若是不大时在配置的间隔时间内能够执行完也是可使用的。.net

@Scheduled(fixedRate = 1000)
    public void fixedRate() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println(String.format("1第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date(System.currentTimeMillis()))));
    }
复制代码
initialDelay属性

该属性的做用是第一次执行延迟时间code

@Scheduled(initialDelay=1000,fixedDelay = 3000)
    public void initialDelay() {
        System.out.println(String.format("1第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date(System.currentTimeMillis()))));
    }
复制代码

**注意:**上面全部属性的配置时间单位都是毫秒

点击查看SpringBoot从入门到高级视频教程

相关文章
相关标签/搜索