import ballerina/log;
import ballerina/task;

// The Task Timer configuration record to configure the Task Listener.
task:TimerConfiguration timerConfiguration = {
    intervalInMillis: 1000,
    initialDelayInMillis: 3000,
    // Number of recurrences will limit the number of times the timer runs.
    noOfRecurrences: 10
};

// Initialize the listener using the above defined configurations.
listener task:Listener timer = new (timerConfiguration);

int count = 0;

// Creating a service on the task Listener.
service timerService on timer {
    // This resource triggers when the timer goes off.
    resource function onTrigger() {
        log:printInfo("Cleaning up...");
        log:printInfo(count.toString());
        count = count + 1;
    }
}

Task Service Timer

Task Timer Services are used to execute tasks periodically. A timer should have an intervalInMillis specified and an initialDelayInMillis if needed. The delay specifies the initial delay before the task is executed for the first time. Thereafter, the timer triggers at the provided interval. The onTrigger() resource is called when the timer triggers. An optional noOfRecurrences field can be used to provide the maximum number of times the Timer should run before shutting down.

import ballerina/log;
import ballerina/task;
task:TimerConfiguration timerConfiguration = {
    intervalInMillis: 1000,
    initialDelayInMillis: 3000,

The Task Timer configuration record to configure the Task Listener.

    noOfRecurrences: 10
};

Number of recurrences will limit the number of times the timer runs.

listener task:Listener timer = new (timerConfiguration);

Initialize the listener using the above defined configurations.

int count = 0;
service timerService on timer {

Creating a service on the task Listener.

    resource function onTrigger() {
        log:printInfo("Cleaning up...");
        log:printInfo(count.toString());
        count = count + 1;
    }
}

This resource triggers when the timer goes off.

# To start the service, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command below.
ballerina run task_service_timer.bal
2019-09-06 13:37:17,298 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:17,299 INFO  [ballerina/log] - 0
2019-09-06 13:37:18,288 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:18,289 INFO  [ballerina/log] - 1
2019-09-06 13:37:19,287 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:19,289 INFO  [ballerina/log] - 2
2019-09-06 13:37:20,288 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:20,289 INFO  [ballerina/log] - 3
2019-09-06 13:37:21,287 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:21,288 INFO  [ballerina/log] - 4
2019-09-06 13:37:22,287 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:22,287 INFO  [ballerina/log] - 5
2019-09-06 13:37:23,287 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:23,288 INFO  [ballerina/log] - 6
2019-09-06 13:37:24,287 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:24,287 INFO  [ballerina/log] - 7
2019-09-06 13:37:25,288 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:25,289 INFO  [ballerina/log] - 8
2019-09-06 13:37:26,285 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:37:26,286 INFO  [ballerina/log] - 9

The onTrigger() function is triggered every second but with an initial delay of 3 seconds.