First add annotation on the startup class: @EnableScheduling to start the timing task
@SpringBootApplication@EnableSchedulingpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Then create a new timed task class
@Componentpublic class QuartzService { /** * Execute timing tasks through time expressions*/ @Scheduled(cron = "0 0/1 * * * ?") public void timerToNow(){ System.out.println("now time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } /** *Execute once every X milliseconds after starting time point*/ @Scheduled(fixedRate = 5000) public void timerToZZP(){ System.out.println("fixedRate:" + new Random().nextLong() + new SimpleDateFormat("HH:mm:ss").format(new Date())); } /** * Execute once every X milliseconds after the end time point*/ @Scheduled(fixedDelay = 10000) public void timerToReportCount(){ System.out.println("fixedDelay:" + new Random().nextLong() + new SimpleDateFormat("HH:mm:ss").format(new Date())); } /** * The first delay is X milliseconds, and then execute every X milliseconds according to the fixedRate rules */ @Scheduled(initialDelay = 10000,fixedRate = 6000) public void timerToReport(){ System.out.println("initialDelay:" + new Random().nextLong() + new SimpleDateFormat("HH:mm:ss").format(new Date())); }}Start the project, start the scheduled task
Summarize
The above is the implementation code of springBoot timed task processing that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!