Examples of Spring Boot and Kotlin scheduled Tasks (Scheduling Tasks) and kotlinscheduling

Source: Internet
Author: User

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:

  1. @ Scheduled (fixedRate = 1000): 1 second after the last execution start time
  2. @ Scheduled (fixedDelay = 1000): 1 second after the last execution completion time
  3. @ 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.
  4. @ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.