美文网首页
定时任务

定时任务

作者: yinhaijun | 来源:发表于2019-01-22 12:17 被阅读10次

springboot中的定时任务

1.在启动类上加入@EnableScheduling开启定时任务

@ComponentScan(value = "com.haijunyin.springbootdemo")
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableAsync
@EnableScheduling   //开启定时任务
public class SpringbootdemoSimpleApplication extends SpringBootServletInitializer{

2.新建类HelloJob.java

package com.haijunyin.springbootdemo.simple.controller.jobs;

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

import java.text.*;
import java.util.Date;

@Component
public class HelloJob {

    public final static long ONE_Minute = 60 * 1000;
    public final static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 每一分钟执行一次,需要在业务执行完之后重新计时
     */
    @Scheduled(fixedDelay = ONE_Minute)
    public void fixedDelayJob(){
        System.out.println(format.format(new Date())+" >>fixedDelay执行....");
    }

    /**
     * 每一分钟执行一次,无论上次任务是否执行完
     */
    @Scheduled(fixedRate = ONE_Minute)
    public void fixedRateJob(){
        System.out.println(format.format(new Date())+" >>fixedRate执行....");
    }

    /**
     * 每天3点15执行一次
     */
    @Scheduled(cron="0 15 3 * * ?")
    public void cronJob(){
        System.out.println(format.format(new Date())+" >>cron执行....");
    }
}

3.启动springboot,得到输出结果

2019-01-22 12:09:00 >>fixedRate执行....
2019-01-22 12:09:00 >>fixedDelay执行....
2019-01-22 12:10:00 >>fixedRate执行....
2019-01-22 12:10:00 >>fixedDelay执行....
2019-01-22 12:11:00 >>fixedRate执行....
2019-01-22 12:11:00 >>fixedDelay执行....
2019-01-22 12:12:00 >>fixedRate执行....
2019-01-22 12:12:00 >>fixedDelay执行....

corn表达式

* 第一位,表示秒,取值0-59
* 第二位,表示分,取值0-59
* 第三位,表示小时,取值0-23
* 第四位,日期天/日,取值1-31
* 第五位,日期月份,取值1-12
* 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思
          另外:1表示星期天,2表示星期一。
* 第7为,年份,可以留空,取值1970-2099
(*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...
(?)问号:问号只能出现在日期和星期这两个位置,表示这个位置的值不确定,每天3点执行,所以第六位星期的位置,我们是不需要关注的,就是不确定的值。同时:日期和星期是两个相互排斥的元素,通过问号来表明不指定值。比如,1月10日,比如是星期1,如果在星期的位置是另指定星期二,就前后冲突矛盾了。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
0 0 3 * * ?     每天3点执行
0 5 3 * * ?     每天3点5分执行
0 5 3 ? * *     每天3点5分执行,与上面作用相同
0 5/10 3 * * ?  每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行
0 10 3 ? * 1    每周星期天,3点10分 执行,注:1表示星期天    
0 10 3 ? * 1#3  每个月的第三个星期,星期天 执行,#号只能出现在星期的位置

原文参考spring boot项目中处理Schedule定时任务

相关文章

  • 2019-07-31定时任务

    定时任务 定时任务实现方法 系统默认定时任务 用户自定义设置定时任务 定时任务配置文件 定时任务启动 定时任务样例...

  • 分布式定时调度-xxl-job

    一、定时任务概述 1.1 定时任务认识 1.1.1 什么是定时任务 定时任务是按照指定时间周期运行任务。使用场景为...

  • day 22 操作系统定时任务

    系统定时任务概念==生活中闹钟 系统定时任务实现方法: 实现定时任务配置: 定时任务如何进行设置 定时任务编写常见...

  • 7月30日 定时任务

    定时任务 代替人自动完成一些任务 定时任务实现的方法 定时任务软件:cronie定时任务软件:atd --- 设...

  • SpringBoot 定时任务

    1.如何定时任务 1.1 开启定时任务 1.2 @Scheduled(预定的)选择要定时执行的任务 == 定时在前...

  • crontab 定时任务

    查看当前用户的定时任务列表 创建(编辑)定时任务列表 定时任务格式 删除定时任务 注意 一定要设置crontab的...

  • 2019-10-14 定时任务方案

    定时任务方案 定时任务实现

  • Linux定时任务Crontab

    定时任务服务提供crontab命令来设定任务 定时任务命令: 定时任务服务提供crontab命令来设定任务 cro...

  • Android中 Handler延时 定时任务

    1.延时 2.定时任务,间隔固定时间执行某项任务 3.定时任务,间隔固定时间执行某项操作后关闭定时任务 参考:ht...

  • crondtab 定时任务

    编辑定时任务 crontab -e 查看定时任务 crontab -l 删除定时任务 crontab -r 如:*...

网友评论

      本文标题:定时任务

      本文链接:https://www.haomeiwen.com/subject/jnjljqtx.html