import ballerina/log;
import ballerina/task;

// Task Appointment configuration record of the Task Listener.
// Task Appointment can either have a CRON expression (`string`) or an
// `AppointmentData` record for the `appointmentData` field. Optionally, a
// `noOfRecurrences` can be provided to limit the number of executions.
task:AppointmentConfiguration appointmentConfiguration = {
    // This cron expression will schedule the appointment every second.
    appointmentDetails: "* * * * * ?",
    // Number of recurrences will limit the number of times the timer runs.
    noOfRecurrences: 10
};

// Initialize the listener using pre defined configurations.
listener task:Listener appointment = new (appointmentConfiguration);

int count = 0;

// Creating a service on the task Listener.
service appointmentService on appointment {
    // This resource triggers when the appointment is due.
    resource function onTrigger() {
        log:printInfo("Cleaning up...");
        log:printInfo(count.toString());
        count = count + 1;
    }
}

Task Service Appointment

Task Appointment Services are used to schedule and execute tasks. An appointment should have an appointmentData field. It can be either a string representing a CRON expression or an AppointmentData record. An optional noOfRecurrences field can be used to provide the maximum number of times the appointment should run before shutting down.

import ballerina/log;
import ballerina/task;
task:AppointmentConfiguration appointmentConfiguration = {

Task Appointment configuration record of the Task Listener. Task Appointment can either have a CRON expression (string) or an AppointmentData record for the appointmentData field. Optionally, a noOfRecurrences can be provided to limit the number of executions.

    appointmentDetails: "* * * * * ?",

This cron expression will schedule the appointment every second.

    noOfRecurrences: 10
};

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

listener task:Listener appointment = new (appointmentConfiguration);

Initialize the listener using pre defined configurations.

int count = 0;
service appointmentService on appointment {

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 appointment is due.

# To start the service, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command below.
ballerina run task_service_appointment.bal
2019-09-06 13:35:28,259 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:28,260 INFO  [ballerina/log] - 0
2019-09-06 13:35:29,004 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:29,004 INFO  [ballerina/log] - 1
2019-09-06 13:35:30,003 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:30,003 INFO  [ballerina/log] - 2
2019-09-06 13:35:31,005 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:31,006 INFO  [ballerina/log] - 3
2019-09-06 13:35:32,003 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:32,003 INFO  [ballerina/log] - 4
2019-09-06 13:35:33,003 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:33,003 INFO  [ballerina/log] - 5
2019-09-06 13:35:34,003 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:34,003 INFO  [ballerina/log] - 6
2019-09-06 13:35:35,003 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:35,003 INFO  [ballerina/log] - 7
2019-09-06 13:35:36,006 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:36,006 INFO  [ballerina/log] - 8
2019-09-06 13:35:37,006 INFO  [ballerina/log] - Cleaning up...
2019-09-06 13:35:37,007 INFO  [ballerina/log] - 9

The onTrigger() function is triggered every two seconds starting from the 0th second of a minute. The sample will print logs once every two seconds.