Spring Boot and Kotlin Timing Tasks

In writing Spring Boot applications, you will encounter such scenarios as: you need to send some messages, e-mails and other operations on a regular basis, or you may regularly check and monitor some signs, parameters and so on.

Create timed tasks

Writing timing tasks in Spring Boot is very simple. Here is an example of how to create timing tasks in Spring Boot to output the current time every five seconds.

Add the @EnableScheduling annotation to Spring Book's main class to enable the configuration of timed tasks


import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.scheduling.annotation.EnableScheduling


/**
 * Created by http://quanke.name on 2018/1/12.
 */


@SpringBootApplication
@EnableScheduling
class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
}

Creating Timing Task Implementation Classes


import org.apache.commons.logging.LogFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.text.SimpleDateFormat
import java.util.*


/**
 * Created by http://quanke.name on 2018/1/12.
 */
@Component
class ScheduledTasks {

    val log = LogFactory.getLog(ScheduledTasks::class.java)!!

    private val dateFormat = SimpleDateFormat("HH:mm:ss")

    @Scheduled(fixedRate = 1000)
    fun reportCurrentTime() {
        log.info("present time , ${dateFormat.format(Date())}")
    }
}

Running the program, the console can see the output similar to the following, the timing task began to work properly.

2018-01-21 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)
2018-01-21 23:09:02.112  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : present time , 23:09:02
2018-01-21 23:09:03.042  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : present time , 23:09:03
2018-01-21 23:09:04.042  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : present time , 23:09:04
2018-01-21 23:09:05.042  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : present time , 23:09:05

Scheduled Details

In the above introductory example, the @Scheduled(fixedRate = 1000) annotation is used to define tasks that are performed every second. The use of @Scheduled can be summarized in the following ways:

  • Scheduled(fixedRate = 1000): Execute one second after the last execution point
  • Scheduled(fixedDelay = 1000): Execute one second after the last execution point
  • Scheduled (initial Delay = 1000, fixedRate = 5000): Execute after the first delay of one second, then execute every five seconds according to the fixedRate rule
  • Scheduled(cron="*/1* * * * * *"): Define rules through cron expressions

More Spring Boot and kotlin related content

Welcome to your attention. Spring Boot and kotlin Actual Warfare

Welcome to Public Number ** Full Stack Architecture **

The whole stack is risky, and rookies need to be cautious

Tags: Spring Java Apache jvm

Posted on Tue, 18 Dec 2018 20:12:04 -0800 by Stickdragon