Spring Boot定时任务之传参

xiaoxiao2021-02-28  94

在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; @Component public 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())); } }

注解参数包括:

fixedRatefixedDelay

cron=”…”

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

@Scheduled(cron="${scheduled.repays}")
转载请注明原文地址: https://www.6miu.com/read-84008.html

最新回复(0)