1. 程式人生 > >Spring Boot定時任務之傳參

Spring Boot定時任務之傳參

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

註解引數包括:

  • fixedRate
  • fixedDelay
  • cron=”…”

    可在配置檔案中引數化任務啟動間隔、啟動引數

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