在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())); } }注解参数包括:
fixedRatefixedDelaycron=”…”
可在配置文件中参数化任务启动间隔、启动参数
@Scheduled(cron="${scheduled.repays}")