PackageCom.zooper.demo;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component; @Component Public classScheduledtasks {Private Static FinalLogger log = Loggerfactory.getlogger (scheduledtasks.class); Private Static FinalSimpleDateFormat DateFormat =NewSimpleDateFormat ("HH:mm:ss"); /*** Executed every 5 seconds*/@Scheduled (fixedrate= 5000) Public voidReportcurrenttime () {Log.info ("The time is now {}", Dateformat.format (NewDate ())); } /*** Trigger timed task According to cron expression format * Cron expression format: * 1.Seconds Minutes Hours dayofmonth Month DayofWeek Year * 2.Seconds Minutes Hours dayofmonth Month DayofWeek * Order: * seconds (0~59) * minutes (0~59) * Hours (0~2 3) * Days (months) (0~31, but you need to consider the number of days of your month) * MONTH (0~11) * Day (week) (1~7 1=sun or Sun,mon,tue,wed,thu,fri,sat) * Year (1970-2099) * Note: Each element can be a value (such as 6), a continuous interval (9-12), a time interval (8-18/4) (/= every 4 hours), a list (1,3,5), and a wildcard character. * Because the "date in month" and "Day of the Week" are mutually exclusive, one of the two elements must be set?. */@Scheduled (Cron= "5 * * * *?") Public voidcronscheduled () {Log.info ("Cron:the time is now {}", Dateformat.format (NewDate ())); }}
Enable Scheduled Tasks
PackageCom.zooper.demo;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.scheduling.annotation.EnableScheduling, @SpringBootApplication @enablescheduling Public classApplication { Public Static voidMain (string[] args)throwsException {springapplication.run (application.class); }}
Spring Boot Configuration Timer task