CronScheduler

class CronScheduler(threads: Int = getRuntime().availableProcessors())

Scheduler to execute tasks repeatedly. After using it, you should call the shutdown method. If the JVM finishes without calling shutdown, it will be called upon JVM termination.

Parameters

threads

Number of threads used by the thread pool. By default, it is equals to the number of processors.

Samples

import kotlin.test.Test
import java.lang.Thread.sleep
import kotlin.test.assertEquals

fun main() { 
   //sampleStart 
   // sample
val cron = CronScheduler()
val times = 1
var count = 0

// Increments the counter by one each second
cron.schedule("0/1 * * * * ?") {
    count++
}

sleep((times * 1_000) + 200L)
cron.shutdown()
assertEquals(count, times)
// sample 
   //sampleEnd
}

Constructors

Link copied to clipboard
constructor(threads: Int = getRuntime().availableProcessors())

Functions

Link copied to clipboard
fun schedule(cronExpression: String, callback: () -> Unit)

Schedules a block of code to be executed repeatedly by a Cron expression.

Link copied to clipboard
fun shutdown()

Shuts down this scheduler's thread pool. Calling over an already closed scheduler does not have any effect. It is called by the JVM when it is shut down.