Spring Boot定时任务之传参

来源:互联网 发布:exit code 139 linux 编辑:程序博客网 时间:2024/06/05 02:32

在Spring Boot中我们可以使用@Scheduled注解实现定时任务

import java.text.SimpleDateFormat;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ScheduledTasks {    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");    @Scheduled(fixedRate = 5000)    public void reportCurrentTime() {        log.info("The time is now {}", dateFormat.format(new Date()));    }}

注解参数包括:

  • fixedRate
  • fixedDelay
  • cron=”…”

    可在配置文件中参数化任务启动间隔、启动参数

 @Scheduled(cron="${scheduled.repays}")