spring boot:Scheduling Tasks
程序员文章站
2022-05-01 14:33:12
...
1、you can create a scheduled task
package hello;
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");
//You can also use @Scheduled(cron=". . .") expressions for more sophisticated task scheduling
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
2、Enable Scheduling:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
上一篇: 夏季上火流鼻血怎么办 多吃这些清热降火
下一篇: 贫血怎么办?这6种食物最好不要吃!