美文网首页
定时任务

定时任务

作者: BySjm | 来源:发表于2020-01-11 20:13 被阅读0次

SpringTask

使用

  • 在SpringBoot的引导类上加开启定时任务的注解@EnableScheduling

实例代码:每秒输出当前时间

第一种写法
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TaskTest {
    @Scheduled(cron = "0/1 * * * * ?")
    public void test() {
        log.debug(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
    }
}
第二种写法
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TaskTest {
    @Scheduled(fixedDelay = 1000)
    //@Scheduled(fixedDelayString = "1000")
    public void test2() {
        log.debug(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
    }
}
第三种写法
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TaskTest {
    @Scheduled(fixedRate = 1000)
    public void test3() {
        log.debug(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
    }
}

@Scheduled(fixedDelay = 1000)和@Scheduled(fixedRate = 1000)的区别

  • fixedDelay积压的任务不会执行,任务会丢失。fixedRate会执行积压的任务,任务不会丢失
  • cron表达式的写法默认和fixedDelay一样。积压的任务不会执行,任务会丢失

任务线程池

spring
  task:
    scheduling:
       pool:
         size: 10

相关文章

  • 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/xrybactx.html