First, add the service class ScheduledTaskService
package com.uos.schedule.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; @Service public class ScheduledTaskService { private static final SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private Integer count1 = 1; private Integer count2 = 1; private Integer count3 = 1; @Scheduled (fixedRate = 60000 ) public void scheduledTaskImmediately () { System.out.println (String.format ( "% s of fixedRate execution, when time is:% s", count1 is ++, simpleDataFormat.format ( new new a Date ()) )); } @Scheduled (fixedRate = 60000 ) public void scheduledTaskAfterSleep () { System.out.println (String.format ( "% s of FIXEDDELAY execution, when time is:% s", count2 ++, simpleDataFormat.format ( new new DATE ()))); } @Scheduled (the cron = "* * * * * 0" ) public void scheduledTaskCron () { System.out.println (String.format ( "% s of execution Cron, when time is:% s", count3 ++, simpleDataFormat.format ( new new a Date ()))); } }
Second, add annotations in the main class @EnableScheduling