Scheduling periodic tasks in the cloud microservices and reference cloud architectures
21. 6. 2024
Solutions always required regularly executed and schedulled jobs which performed various tasks.
These tasks can be sending data in regular time, cleansing database and remove unneeded objects, resending data, correcting data, checking for states of the processes, components, data. In the Oracle Siebel CRM world these are known as batch job or repeating components, in the linux world they are known as cron jobs.
For the onpremise services often some external schedullers are used such as Oracle Scheduller, dbms_scheduller etc. The schedulled tasks are also called from operating system, in linux cron tab
In this blog we will discuss the posibilities for schedulled tasks in the JAVA Quarkus framework, which we use to develop backend microservices in the cloud.
JAVA Quarkus has 2 possibilities to programatically call schedulled tasks :
- Quarkus Scheduler
- Quarkus Quartz.
They have different approaches and the selection needs to be good and preciselly analysed.
Quarkus Scheduler
Quarkus Scheduler runs parallel in all nodes. Considering microservices they always are high scalable running in kubernetes cluster in the pods. It cannot retry on failed or skipped executions. You can have scalled more pods running the same microservice based on load. Using Quarkus Scheduller the task will run in every pod and instance.
More information can be found here >
Quarkus Quartz
Quarkus Quartz is sophisticated open source scheduler which can run in clustered mode on one node. Quarz can retry for skipped executions. It is deploying structured DB schema which is used to monitor, orchestrate and log job executions.
It has a variety of possibilities to schedulle the task from annotation , to injecting the scheduling into bean and also crontab like syntax. It works with many DB used by microservice in the cloud.
More information here
package ch.mobi.dki.integration.control;
import ch.mobi.dki.integration.entity.Task;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import io.quarkus.scheduler.Scheduled;
import lombok.Setter;
import org.slf4j.Logger;
import java.time.Instant;
@ApplicationScoped
public class TaskBean {
@Setter
@Inject
Logger logger;
@Transactional
@Scheduled(every = "10s", identity = "task-job")
void schedule() {
Task task = new Task();
//write your code here
logger.info("[ID-SCAN] Cronjob executed.");
task.persist();
}
}
Here is an example how the programatically scheduled job can look like
@Scheduled(cron="0 15 10 * * ?")
void cronJob(ScheduledExecution execution) {
counter.incrementAndGet();
System.out.println(execution.getScheduledFireTime());
}
The DB structure of Quartz framework created in the quarkus project
New Endpoint
@GET
@Path("/tasks")
@Operation(summary = "Get list of tasks executed by cronjob")
@APIResponse(responseCode = "200", description = "Successful")
@Produces(MediaType.APPLICATION_JSON)
@Performance(concurrency = 3000, availability = DAYS7_HOURS24, responseTime = @Timing(percentile99 = 30, percentile95 = 20, percentile90 = 10))
public List<Task> listAll() {
return Task.listAll();
will list from DB executed tasks :
To provision the project db scheme is to be provisioned. Provided scripts were not working for me :
https://github.com/quartz-scheduler/quartz/blob/main/quartz/src/main/resources/org/quartz/impl/jdbcjobstore/tables_sqlServer.sql
https://github.com/quarkusio/quarkus-quickstarts/blob/main/quartz-quickstart/src/main/resources/db/migration/V2.0.0__QuarkusQuartzTasks.sql
Here is the working version : V2.0.0__quartz_tables.sql
OCI CI/CD reference Architecture for Quarkus Microservice
Here is Oracle Cloud Infrastructure reference architecture how to deploy CI/CD pipeline for microservice in OCI :
https://docs.oracle.com/en/learn/oci-devops-cicd-on-kubernetes/index.html#task-1-set-up-the-oci-devops-project
AWS Reference Architecture to deploy Quarkus as Lambda
https://quarkus.io/guides/aws-lambda
https://aws.amazon.com/blogs/architecture/deploy-quarkus-based-applications-using-aws-lambda-with-aws-sam/
Conclusion
Quarkus framework has more approaches to implement fast and efficient schedulled task in the microservice. Repeating task are important part of modern microservices and this blog should navigate you to choose richt approach. It also gives taste how the Devops architecture for microservice in the cloud can look like. CCW has certified cloud development consultant which can help you start your cloud journey, adopt existing workloads to cloud and re-build them with cloud approach only.
Späť na Blog