Examples of Spring Boot and Kotlin scheduled Tasks (Scheduling Tasks) and kotlinscheduling
You may encounter such scenarios when writing Spring Boot applications. For example, you need to regularly send text messages, emails, and other operations, and you may also regularly check and monitor some labels and parameters.
Create scheduled task
Writing a scheduled task in Spring Boot is very simple. The following describes how to create a scheduled task in Spring Boot through an instance to output the current time every five seconds.
Add the @ EnableScheduling annotation to the main class of Spring Boot to enable the configuration of scheduled tasks.
import org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.scheduling.annotation.EnableScheduling/*** Created by http://quanke.name on 2018/1/12.*/@SpringBootApplication@EnableSchedulingclass Applicationfun main(args: Array<String>) {SpringApplication.run(Application::class.java, *args)}
Create scheduled task implementation class
Import org. apache. commons. logging. logFactoryimport org. springframework. scheduling. annotation. scheduledimport org. springframework. stereotype. componentimport java. text. simpleDateFormatimport java. util. */*** Created by http://quanke.name on. * // @ Componentclass ScheduledTasks {val log = LogFactory. getLog (ScheduledTasks: class. java )!! Private val dateFormat = SimpleDateFormat ("HH: mm: ss") @ Scheduled (fixedRate = 1000) fun reportCurrentTime () {log.info ("current time, $ {dateFormat. format (Date ())}")}}
Run the program. The following output is displayed on the console. The scheduled task starts to work normally.
23:09:01. 112 INFO 23832-[main] n. q. kotlin. chaper11_8_1.ApplicationKt: Started ApplicationKt in 8.024 seconds (JVM running for 8.724)
23:09:02. 112 INFO 23832-[pool-2-thread-1] n. q. k. chaper11_8_1.task.ScheduledTasks: current time, 23:09:02
23:09:03. 042 INFO 23832-[pool-2-thread-1] n. q. k. chaper11_8_1.task.ScheduledTasks: current time, 23:09:03
23:09:04. 042 INFO 23832-[pool-2-thread-1] n. q. k. chaper11_8_1.task.ScheduledTasks: current time, 23:09:04
23:09:05. 042 INFO 23832-[pool-2-thread-1] n. q. k. chaper11_8_1.task.ScheduledTasks: current time, 23:09:05
@ Scheduled
In the preceding example, the @ Scheduled (fixedRate = 1000) annotation is used to define the task executed every one second. The following methods can be summarized for the use of @ Scheduled:
- @ Scheduled (fixedRate = 1000): 1 second after the last execution start time
- @ Scheduled (fixedDelay = 1000): 1 second after the last execution completion time
- @ Scheduled (initialDelay = 1000, fixedRate = 5000): it is executed after the first delay of 1 second, and then executed every 5 seconds according to the fixedRate rule.
- @ Scheduled (cron = "/1"): defines a rule using a cron expression.
@ Scheduled annotation is single-threaded. If multithreading is required, add @ Async.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.