Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Manage sheduled jobs
The task
library provides functions to manage the scheduled jobs such as pause, resume, unscheduled, etc.
For more information on the underlying module, see the task
module.
import ballerina/io;
import ballerina/lang.runtime;
import ballerina/task;
import ballerina/time;
// Creates a job to be executed by the scheduler.
class Job {
*task:Job;
int i = 1;
string jobIdentifier;
// Executes this function when the scheduled trigger fires.
public function execute() {
self.i += 1;
io:println(self.jobIdentifier + ", MyCounter: ", self.i);
}
isolated function init(int i, string jobIdentifier) {
self.i = i;
self.jobIdentifier = jobIdentifier;
}
}
public function main() returns error? {
// Gets the current time.
time:Utc currentUtc = time:utcNow();
// Increases the time by three seconds to set the starting delay for the scheduling job.
time:Utc newTime = time:utcAddSeconds(currentUtc, 5);
// Gets the `time:Civil` for the given time.
time:Civil time = time:utcToCivil(newTime);
// Schedules the tasks to execute the job every second.
task:JobId id1 = check task:scheduleJobRecurByFrequency(
new Job(0, "1st Job"), 1);
task:JobId id2 = check task:scheduleJobRecurByFrequency(
new Job(0, "2nd Job"), 3);
// Schedules the one-time job at the specified time.
_ = check task:scheduleOneTimeJob(new Job(0, "3rd Job"), time);
// Waits for 3 seconds.
runtime:sleep(3);
// Gets all the running jobs.
task:JobId[] result = task:getRunningJobs();
io:println("No of running jobs: ", result.length());
// Pauses the specified job.
check task:pauseJob(id1);
io:println("Pasused the 1st job.");
// Waits for 3 seconds.
runtime:sleep(3);
// Resumes the specified job.
check task:resumeJob(id1);
io:println("Resumed the 1st job.");
// Gets all the running jobs.
result = task:getRunningJobs();
io:println("No of running jobs: ", result.length());
// Waits for 3 seconds.
runtime:sleep(3);
// Unschedules the jobs.
check task:unscheduleJob(id1);
check task:unscheduleJob(id2);
}
To run this sample, use the bal run
command.
$ bal run manage_scheduled_jobs.bal1st Job, MyCounter: 12nd Job, MyCounter: 11st Job, MyCounter: 21st Job, MyCounter: 31st Job, MyCounter: 42nd Job, MyCounter: 2No of running jobs: 3Pasused the 1st job.3rd Job, MyCounter: 12nd Job, MyCounter: 3Resumed the 1st job.1st Job, MyCounter: 51st Job, MyCounter: 6No of running jobs: 21st Job, MyCounter: 71st Job, MyCounter: 81st Job, MyCounter: 91st Job, MyCounter: 102nd Job, MyCounter: 4
PreviousSchedule one time job
NextGenerate UUID